NotificationCompat详解

    • 什么是Notification
      • 系统通知区域
      • 系统通知展开
    • 创建一个通知
    • 创建可展开的通知
    • 带进度条通知

什么是Notification

可以显示在自己的应用程序之外,而显示系统的桌面通知栏上,经常用于消息通知,软件更新等

他有两种表现形式

系统通知区域

系统通知展开

NotificationCompat详解_第1张图片

创建一个通知

通常创建一个通知的流程是通过NotificationCompat.Builder对象创建一个Build,然后调用build()方法返回一个Notification对象,最后使用NotificationManager.notify()显示一个通知

一个Notification对象必须包含:

  1. 一个小的图标, 通过setSmallIcon()方法设置
  2. 一个标题, 通过setContentTitle()方法设置
  3. 详细文本, 通过setContentText()方法设置

其他可选的内容和设置可参考NotificationCompat.Builder

创建一个简单的通知

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
//设置点击通知跳转的activity
Intent resultIntent = new Intent(this, ResultActivity.class);

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_CLEAR_TASK);

//创建一个任务栈,用于处理在通知页面,返回时现实的页面
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

final Notification notification = mBuilder.build();

//这通知的其他属性,比如:声音和振动
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;

mNotificationManager.notify(mId, notification);

activity配置

<activity  android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity  android:taskAffinity="aa.bb" android:excludeFromRecents="true" android:name=".ResultActivity" android:label="@string/title_activity_result" android:parentActivityName=".MainActivity" android:theme="@style/AppTheme.NoActionBar">
    <meta-data  android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" />
</activity>

效果如图所示

注意:经测试TaskStackBuilder在部分手机上没有作用(红米2 note),其他手机可以(华为p7,红米note)

创建可展开的通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Event tracker")
        .setContentText("Events received");
NotificationCompat.InboxStyle inboxStyle =
        new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Moves events into the expanded layout
for (int i = 0; i < events.length; i++) {

    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);

mNotificationManager.notify(2, mBuilder.build());

默认是关闭的,需要双指在通知上向下滑动,才能展开,不过我觉得这功能太鸡肋了,那个用户没事闲着蛋疼挨个挨个试那个通知能展开啊。

带进度条通知

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
        .setContentText("Download in progress")
        .setSmallIcon(R.mipmap.ic_launcher);

//启动一个线程,定时更新进度条
new Thread(
        new Runnable() {
            @Override
            public void run() {
                int incr;
                // Do the "lengthy" operation 20 times
                for (incr = 0; incr <= 100; incr += 5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotificationManager.notify(0, mBuilder.build());
                    // Sleeps the thread, simulating an operation
                    // that takes time
                    try {
                        // Sleep for 5 seconds
                        Thread.sleep(1 * 1000);
                    } catch (InterruptedException e) {
                    }
                }
                // 下载完成,更新信息
                mBuilder.setContentText("Download complete")
                        // 移除进度条
                        .setProgress(0, 0, false);
                mNotificationManager.notify(0, mBuilder.build());
            }
        }
// Starts the thread by calling the run() method in its Runnable
).start();

参考:http://www.tuicool.com/articles/jEr6Zjm

你可能感兴趣的:(android,Notificati)