<main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"
android:textSize="12sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"
android:textSize="12sp"/>
</LinearLayout>
<MainActivity.java>
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
static final int NOTIFICATION_ID=0x1123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取应用界面中的Button对象
Button bn=(Button) findViewById(R.id.bn);
//为按钮的单击事件绑定事件监听器
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建一个启动其他Activity的Intent
Intent intent=new Intent(MainActivity.this,
QtActivity.class);
PendingIntent pi =PendingIntent.getActivity(MainActivity.this
, 0, intent,PendingIntent .FLAG_UPDATE_CURRENT);
//创建一个Notification
Notification notify=new Notification();
//为Notification设置图标,该图标显示在状态栏
notify.icon=R.drawable.ic_launcher;
//设置文本内容显示在状态栏
notify.tickerText="通知";
//设置发生时间
notify.when=System.currentTimeMillis();
//设置声音
notify.defaults=Notification.DEFAULT_SOUND;
//设置默认声音 默认震动 默认闪光灯
notify.defaults=Notification.DEFAULT_ALL;
//设置事件信息
notify.setLatestEventInfo(MainActivity.this,
"普通通知", "点击查看", pi );
//获取系统的NotificationManager服务
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(NOTIFICATION_ID,notify);
}
});
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//获取系统的NotificationManager服务
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//quxiaotongzhi
notificationManager.cancel(NOTIFICATION_ID);
}
});
}
}