美文网首页Notification
Android Notification 使用

Android Notification 使用

作者: helloKai | 来源:发表于2016-06-26 13:48 被阅读286次

http://developer.android.com/training/notify-user/build-notification.html

  1. 创建Notification
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("hello world!");
  1. 对Notification添加Action
Intent resultIntent = newIntent(this,ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
        this,                
        0,                
        resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//设置点击行为,和Notification相关联起来
mBuilder.setContentIntent(resultPendingIntent);
  1. 发布一个Notification
//设置ID为001?
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//要在setContentIntent之后完成
mNotifyMgr.notify(mNotificationId,mBuilder.build());
  1. 设置一个带有返回栈的Intent,不会返回到MainActivity中
<activity    
        android:name=".ResultActivity"
        android:parentActivityName=".MainActivity">    
        <meta-data        
            android:name="android.support.PARENT_ACTIVITY"        
            android:value=".MainActivity" />
</activity>

再设置Intent

Intent resultIntent = new Intent(this,ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//添加到返回栈中
stackBuilder.addParentStack(ResultActivity.class);
//添加Intent到栈顶
stackBuilder.addNextIntent(resultIntent);
//获得带有整个返回栈的PendingIntent
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
//Notification和Intent进行绑定
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id,mBuilder.build());
  1. 创建一个特定的PendingIntent,按back会返回到之前的MainActivity
    首先要在activity中添加两个属性
    android:taskAffinity
    android:excludeFromRecents代表是否将它从最近使用中移除
android:taskAffinity=""
android:excludeFromRecents="true"
Intent notifyIntent = new Intent(Intent.makeMainActivity(new ComponentName(this, ResultActivity.class)));
//将用来放置activity的task清空,然后再在task中启动
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(notifyPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id,mBuilder.build());
  1. 更新Notification内容只要知道id,再通过notify()即可
//Modify a Notification,by calling NotificationManager.notify(ID,notification);
int numMessages = 0;
mBuilder.setContentText("3").setNumber(++numMessages);
mNotificationManager.notify(id,mBuilder.build());
  1. 删除Notification有四种方法
    (1) 用户通过点击"Clear All"
    (2) 在设置Notification的时候setAutoCancel(true);
    (3) 通过调用cancel();
    (4) 通过调用cancelAll();移除以前该应用的所有通知
  2. 设置Big View
    创建两个Action
Intent dismissIntent = new Intent(this,PingService.class);
dismissIntent.setAction("com.example.hellokai..ACTION_DISMISS");
PendingIntent piDismiss = PendingIntent.getService(this,0,dismissIntent,0);
Intent snoozeIntent = new Intent(this,PingService.class);
snoozeIntent.setAction("com.example.hellokai.ACTION_SNOOZE");
PendingIntent piSnooze = PendingIntent.getService(this,0,snoozeIntent,0);
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("hello")
.setContentText("big view")
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText("hello result"))
.addAction(R.mipmap.ic_launcher,"dismiss",piDismiss)
.addAction(R.mipmap.ic_launcher,"snooze",piSnooze);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(id,builder.build());

9.通知设置进度条

final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            for(incr = 0;incr <= 100;incr+=5) {                                           
                mBuilder.setProgress(100,incr,false); 
                manager.notify(id,mBuilder.build());
                try{
                     Thread.sleep(1*1000);
                }catch(InterruptedException e){
                     e.printStackTrace();                    
                }                
            }                
            //结束之后要重新更新为0,false表示没有进图条动画,true表示有
            mBuilder.setContentText("download complete").setProgress(0,0,false);                                                   
            manager.notify(id,mBuilder.build());            
       }        
}).start();

相关文章

网友评论

  • 乘风破浪的程序员:楼主,您知道原因么? :pray:
    helloKai:@hante 我不太清楚具体的原因,我查了一下相关问题,好多都说是和bitmap的设置过大有关系,http://stackoverflow.com/questions/7988018/custom-notification-java-lang-runtimeexception-bad-array-lengths,看看这个能不能对你有所帮助。
    helloKai:@hante 相关报错代码可以贴一下吗?
  • 乘风破浪的程序员:楼主,我用的NotificationCompat.Builder,但发生了如下错误:

    Fatal Exception: java.lang.RuntimeException: bad array lengths
    at android.os.Parcel.readIntArray(Parcel.java:926)
    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:777)
    at android.app.NotificationManager.notify(NotificationManager.java:272)
    at android.app.NotificationManager.notify(NotificationManager.java:211)
    at cc.netpas.android_firewall.service.BackgroundDaemon$3.run(BackgroundDaemon.java:297)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.os.HandlerThread.run(HandlerThread.java:61)

本文标题:Android Notification 使用

本文链接:https://www.haomeiwen.com/subject/ywnadttx.html