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);
}
}
//为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);
}
}
相关文章推荐
- Android开发系列(十四):ListView用法、对ListView监听的三种方法以及ListView中Adapter的使用方法
- 回退键的监听以及相关的Dialog使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- 接口的定义,使用,以及事件的监听方法
- ListView用法、对ListView监听的三种方法以及ListView中Adapter的使用方法 2014-10-09 0 个评论
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- 列表适配器的使用(以及列表相应的一些监听方法)
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- appwidget的使用方法和配置以及监听
- LinkedHashMap和HashMap的区别以及使用方法
- 纯干货详解iptables工作原理以及使用方法
- Hibernate中get()与load()的区别,以及关于ThreadLocal的使用方法
- BASH 命令以及使用方法小结【转】
- 4000 Oracle游标的使用方法以及使用for循环实现游标的原理
- Hadoop使用常见问题以及解决方法
- JSON和JSONP的区别,以及使用方法
- OC方法之---NSSortDescriptor使用注意以及直接排序字符串数组
- Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
- Android中Preference的使用以及监听事件分析
- Android中Preference的使用以及监听事件分析