您的位置:首页 > 其它

Notification的使用,以及他的监听方法

2011-10-18 09:09 302 查看
public class ActivityMainNotification extends Activity {
//为Notification添加一个标识的ID.
private static int NOTIFICATIONS_ID = 1;//R.layout.activity_notification;
//为Notification添加一个管理的对象NotificationManager
private NotificationManager mNotificationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);

Button button;
//使用NotificationManager的getSystemService(NOTIFICATION_SERVICE)来申明(Notification)
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

button = (Button) findViewById(R.id.sun_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//这里使用了自定的方法,为了实现Notification.
setWeather("晴空万里", "天气预报", "晴空万里", R.drawable.sun);
}
});

button = (Button) findViewById(R.id.cloudy_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setWeather("阴云密布", "天气预报", "阴云密布", R.drawable.cloudy);
}
});

button = (Button) findViewById(R.id.rain_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setWeather("大雨连绵", "天气预报", "大雨连绵", R.drawable.rain);
}
});

button = (Button) findViewById(R.id.defaultSound);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_SOUND);
}
});

button = (Button) findViewById(R.id.defaultVibrate);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_VIBRATE);
}
});

button = (Button) findViewById(R.id.defaultAll);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_ALL);
}
});

button = (Button) findViewById(R.id.clear);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mNotificationManager.cancel(NOTIFICATIONS_ID);
}
});

}

private void setWeather(String tickerText, String title, String content,
int drawable) {
/*第一个参数要显示的图片id
* 第二个参数为显示的文本
* 第三个参数为Notification显示的时间,一般为立即显示
*/
Notification notification = new Notification(drawable, tickerText,
System.currentTimeMillis());
//创建一个动态的intent,为了识别点击的事件处理
//第一个参数是上下文
//第二个参数是事件的序列参数
//第三个参数是intent事件响应
//第四个参数是为intent做序列标记
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityMain.class), 0);
//setLatestEventInfo的第一个参数为上下文
//第二个参数为notification标题
//第三个参数为notification内容
//第四个参数为intent处理
notification.setLatestEventInfo(this, title, content, contentIntent);
//通过NotificationManager的对象来管理notification
mNotificationManager.notify(NOTIFICATIONS_ID, notification);
}

private void setDefault(int defaults) {

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityMain.class), 0);

String title = "天气预报";
String content = "晴空万里";

final Notification notification = new Notification(R.drawable.sun,
content, System.currentTimeMillis());

notification.setLatestEventInfo(this, title, content, contentIntent);
//为Notification设置表现形式
notification.defaults = defaults;

mNotificationManager.notify(NOTIFICATIONS_ID, notification);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: