Notification 使用
来源:互联网 发布:java房屋租赁系统 编辑:程序博客网 时间:2024/06/06 01:51
感谢 http://www.cnblogs.com/tianjian/archive/2012/12/31/2840862.html
Notification在android中的使用比较多,比如消息推送,使用系统的下载管理器下载,音乐播放器最小化......
现在总结一下Notificaiton的使用
public void initNotification() {Intent intent = new Intent(this, ResultActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);/** * getActivity(Context context, int requestCode, Intent intent, int flags) * 这个方法的作用就相当于context.startActivity(intent) */PendingIntent pendingintent = PendingIntent.getActivity(this, 0,intent, 0);//第二个参数表示notificaiton刚刚 显示在状态栏时候显示的文字Notification notification = new Notification(R.drawable.ic_launcher,"first show", 0);RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notificationlayout);notification.contentIntent = pendingintent;notification.contentView = remoteViews;// Notification.FLAG_ONGOING_EVENT 设置notification显示在正在运行栏中,默认是显示在通知栏里面notification.flags = Notification.FLAG_AUTO_CANCEL| Notification.FLAG_ONGOING_EVENT;NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);// 这个方法执行时候 如果该ID对象的notificaiton对象已经显示在状态栏了,那么就用新的notificaiton对象更新之前的那个manager.notify(ID, notification);}
这是最常见的使用方法。
android后来又出来一个NotificationCompat.Builder用来构造Notificaiton
官方网址:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setProgress(int, int, boolean)
这个类提供了很多方法用于构造notificaiton。
下面是例子:
public void initNotificationNew() {NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(this, ResultActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent pendingintent = PendingIntent.getActivity(this, 0,intent, 0);RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notificationlayout);NotificationCompat.Builder builder = new NotificationCompat.Builder(this);//setSmallIcon setContentTitle setContentText是必须的 不定义或少定义都不显示notificaitonbuilder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("NotificationCompat").setContentText("NotificationCompat Text").setContentIntent(pendingintent).setNumber(10).setOngoing(true).setWhen(0).setAutoCancel(true).setContent(remoteViews);//这个方法是显示进度条,这个比较有用builder.setProgress(100, progress, false);manager.notify(ID, builder.build());}
NotificationCompat.Builder还有一个setStyle方法用于设置一个新的界面显示效果,但是好像只支持4.1+系统,使用不多。就不多说了。
- Notification 使用
- Notification使用
- 使用Notification
- Notification 使用
- notification 使用
- Notification使用
- Notification使用
- 使用Notification
- notification使用
- Notification---Notification使用详解之一:基础应用
- Notification的使用(自定义Notification布局)
- 使用 WS-Notification
- 3. Notification的使用
- 3、Notification的使用
- Android-Notification的使用
- Android NOtification 使用
- iphone notification及使用
- Android Notification的使用
- Android 图像处理(类型转换,比例缩放,倒影,圆角)
- Android控件属性一览
- Android照片墙应用实现,再多的图片也不怕崩溃
- Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml
- android上传图片至服务器
- Notification 使用
- __user 修饰符详解
- 轮回!?2013年重回SAP技术,HANA,我来了!
- Android之调用webservice 实现图片上传下载(转)
- Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
- ajp 到底是什么
- python django 创建数据库和自动化admin
- Github初探
- [总结】半年的总结