NotificationCompat详解

什么是Notification

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

他有两种表现形式

系统通知区域

系统通知展开

这里写图片描述

创建一个通知

通常创建一个通知的流程是通过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值