android Notification显示通知的两种方法
来源:互联网 发布:cst电磁仿真软件下载 编辑:程序博客网 时间:2024/05/17 22:19
第一种显示通知的方法:
/** * notification * * @param id */private void showNotification(){RemoteViews views = new RemoteViews(getPackageName(), R.layout.statusbar);views.setImageViewResource(R.id.icon, R.drawable.fm_icon);views.setTextViewText(R.id.fm_run, getString(R.string.fm_run));Notification status = new Notification();status.contentView = views;status.flags |= Notification.FLAG_ONGOING_EVENT;status.icon = R.drawable.ic_fm_status_bar;status.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, FMEntryView.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0);startForeground(PLAYBACKSERVICE_STATUS, status);}
删除通知:
/** * delete notification */private void deleteNotification(){NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);nm.cancel(PLAYBACKSERVICE_STATUS);stopForeground(true);}
第二种显示通知的方法:
public void showNotification(int icon,String tickertext,String title,String content){ //设置一个唯一的ID,随便设置 //Notification管理器 Notification notification=new Notification(icon,tickertext,System.currentTimeMillis()); //后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用) notification.defaults=Notification.DEFAULT_ALL; //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND //振动为Notification.DEFAULT_VIBRATE; //Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应 //全部为Notification.DEFAULT_ALL //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限 PendingIntent pt=PendingIntent.getActivity(this, 0, new Intent(this,main.class), 0); //点击通知后的动作,这里是转回main 这个Acticity notification.setLatestEventInfo(this,title,content,pt); nm.notify(notification_id, notification); } //showNotification(R.drawable.home,"图标边的文字","标题","内容");
删除通知:
nm.cancel(notification_id);
上面此法别忘了增加权限:
<-permission android:name="android.permission.VIBRATE" /> <!-- 允许振动 -->
- android Notification显示通知的两种方法
- Android 通知Notification的两种实现方法
- Notification基本通知的两种写法
- Android开发:notification通知以及通知不显示的问题
- Android开发:notification通知以及通知不显示的问题
- 关于Android Notification.Builder不显示通知的问题
- Android显示不重复通知的Notification,androidnotificatio
- Android Demo Sample: 一种新建Notification通知的方法
- Android 使用Notification自定义通知栏显示
- Android的状态栏通知(Notification)
- Android Notification通知的运用
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- Android的状态栏通知(Notification)
- android通知(Notification)的使用
- Android 的状态栏通知(Notification)
- Android的消息通知--Notification
- 修改MFC标题栏上的图标和修改MFC窗口标题
- 对象序列化
- svn命令行对于同一个目录的同一个属性添加多个属性项
- No curses/termcap library found的解决办法补充:在mysql编译的时候
- 遍历hashmap ---java
- android Notification显示通知的两种方法
- ENGLISH SENTENCES 3
- 10 个非常重要的 HotSpot JVM 参数
- 为用户设计舒适的姿势
- 四六级 5
- socet网络编程1
- 面试题精解之一: 二叉树
- POJ 1328 Radar Installation 贪心
- mysql插入date类型的值 .