通知Notification
来源:互联网 发布:西门子模拟量怎么编程 编辑:程序博客网 时间:2024/05/16 13:46
状态栏通知的简单创建
注:如果下面的发送通知和发送导航通知是跳转到同一个界面,那么可能会发生覆盖问题。
notification跳转的Activity分为常规的和特定的,常规的就是在其他页面也可以跳转进去的;特定的是只用来显示通知的其他内容的Activity
可以创建一个Activity,写上几个Button来手动发送通知
以下方法里一样的类,如(NotificationCompat.Builder、NotificationManager等,可以提出来设为全局的)
(一)发送通知(封装在一个方法里)
public void sendNotification(){ /**构造通知notification的界面*/ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher); //小图标 builder.setContentTitle(getString(R.string.notification_title)); //标题,一般定义在String文件里 builder.setContentText(getString(R.string.notification_content)); //内容 builder.setTicker(getString(R.string.notification_content)); //滚动的内容 builder.setAutoCancel(true); //点击后自动消失 /**通知行为*/ Intent intents = new Intent(this,MediaPlayerScanOneActivity.class);//点击通知时要跳转的Activity//启动的特定Activity在一个新的任务栈中intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); /**发送通知notification*/ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); int notificationId = 1; Notification notification = builder.build(); manager.notify(notificationId, notification); }
(二)更新通知
public void updateNotification(){ //更新哪个通知,就用哪个通知的notificationId /**构造通知notification的界面*/ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.kg_ic_navigation_game); //小图标 builder.setContentTitle("双十一"); //标题 builder.setContentText("抢购"); //内容 builder.setTicker("抢购"); //滚动的内容 builder.setAutoCancel(true); //点击后自动消失 /**通知行为*/ Intent intents = new Intent(this,MusicPlayerMainActivity.class);//点击通知时要跳转的Activity PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); /**发送通知notification*/ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); int notificationId = 1; Notification notification = builder.build(); manager.notify(notificationId, notification); }
(三)清除通知
public void clearNotification(){ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1);//消除某个ID的通知// manager.cancelAll();//消除所有通知 }
(四)发送导航通知(返回时,返回到通知Activity的父类Activity)
public void sendNavigationNotification(){ /**发送导航通知*/ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.kg_ic_navigation_more_ringtone); builder.setContentTitle("降价通知"); builder.setContentText("全场不要钱,快来抢!!"); builder.setTicker("降价通知"); builder.setAutoCancel(true); /**通知行为*/ Intent intents = new Intent(this,MediaPlayerScanOneActivity.class);//点击通知时要跳转的Activity //构造一个返回栈 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MediaPlayerScanOneActivity.class);//同上 stackBuilder.addNextIntent(intents); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); /**发送通知*/ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); int notificationId = 2; Notification notification = builder.build(); manager.notify(notificationId,notification); }
并且在AndroidManifest里写上要跳转的Activity的父类Activity
<activity android:name=".MediaPlayerScanOneActivity" android:parentActivityName=".MusicPlayerMainActivity"></activity>
下载进度的通知
public void download(){ /**构造通知notification的界面*/ final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.kg_scanning_view_scan); //小图标 builder.setContentTitle("下载文件"); //标题,一般定义在String文件里 builder.setContentText("开始下载文件"); //内容 builder.setTicker("开始下载文件"); //滚动的内容 builder.setAutoCancel(true); //点击后自动消失 /**发送通知notification*/ final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final int notificationId = 3; Notification notification = builder.build(); manager.notify(notificationId, notification); //为了不让主线程停顿,在子线程里刷新 new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 100; i++) {// builder.setProgress(max, progress, indeterminate); // max:最大进度 progress:当前进度 indeterminate:false为确定的,true为不确定的(就是不知道什么时候完) builder.setProgress(100, i, false);// builder.setProgress(0, 0, true);//设置进度为不确定模式 builder.setContentText("下载文件中..."); manager.notify(notificationId, builder.build());//刷新通知界面 try { Thread.sleep(100); //间隔100毫秒 } catch (InterruptedException e) { e.printStackTrace(); } } builder.setProgress(0, 0, false); builder.setContentText("下载完成"); manager.notify(notificationId, builder.build());//刷新通知界面 } }).start(); }
BigView的Notification(下面多加了按钮),当上面还有其他通知时,下面的按钮会收起隐藏
public void bigViewNotification(){ /**构造通知notification的界面*/ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.kg_scanning_view_scan); //小图标 builder.setContentTitle("音乐播放器"); //标题,一般定义在String文件里 builder.setContentText("无聊听听歌"); //内容 builder.setTicker("音乐播放器"); //滚动的内容 builder.setAutoCancel(true); //点击后自动消失 builder.setStyle(new NotificationCompat.BigTextStyle().bigText("无聊听听歌")); /**播放动作*/ Intent playIntent = new Intent(this,MediaPlayerMusicService.class); playIntent.putExtra("MUSIC_PATH", "file://"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/xxx.mp3"); PendingIntent playPendingIntent = PendingIntent.getService(this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);// playIntent.setAction(action); //如果两个动作想要启动的是同一个Service或其他东西,要传参就必须要设置Action来区分Intent builder.addAction(R.drawable.widget_play_button_default, "播放", playPendingIntent); /**暂停动作*/ Intent pauseIntent = new Intent(this,MediaPlayerMusicService.class); playIntent.putExtra("MUSIC_PATH", "file://"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/xxx.mp3"); PendingIntent pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);// playIntent.setAction(action); builder.addAction(R.drawable.widget_pause_button_default, "暂停", pausePendingIntent); /**发送通知notification*/ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(4, builder.build()); }
自定义通知
public void MyNotification(){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); //自定义View RemoteViews views = new RemoteViews(getPackageName(), R.layout.myself_notification_layout); //自定义通知必须要设置icon,其他不设置不影响 builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker("启动酷狗音乐"); Intent preIntent = new Intent(this,MediaPlayerMusicService.class); PendingIntent prePendingIntent = PendingIntent.getService(this, 0, preIntent, PendingIntent.FLAG_UPDATE_CURRENT); //通知里的自定义按键的监听 views.setOnClickPendingIntent(R.id.notification_music_pre, prePendingIntent);// views.setOnClickPendingIntent(R.id.notification_music_play, pendingIntent);// views.setOnClickPendingIntent(R.id.notification_music_next, pendingIntent); builder.setContent(views); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(5,builder.build()); }
0 0
- Notification通知
- Notification通知
- Notification(通知)
- Notification通知
- Notification通知
- notification通知
- Notification 通知
- 通知(Notification)
- Notification通知
- 通知notification
- 通知Notification
- 通知 ------- Notification
- 通知Notification
- Notification--通知
- Notification通知
- 通知 notification
- 通知Notification
- Notification通知
- iOS NSDate NSCanlendar NSComponent 概念讲解
- MFC radio单选按钮设置组属性
- 学会总结才能有进步
- .vsd转.eps
- 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 通知Notification
- Android开发中遇到的问题(一)——Android模拟器端口被占用问题的解决办法
- 筹划公益性活动有感
- LeetCode 050 Pow(x, n)
- Spring中@Async用法总结
- 关于 "编码 GBK 的不可映射字符"错误的解决
- Mobile App Developers Are Suffering
- 在做excel导出时如何将excel直接写在输出流中
- C++11随机数