android通知(Notification)的使用

来源:互联网 发布:stc8f单片机 编辑:程序博客网 时间:2024/06/09 20:03

需要注意的几点:

1.通知的类型,一般普通的消息事件和正在进行的事件

2.通知的布局是可以自定义的

3.通知在android4.1以后,还支持一种detail的通知,(就是可扩展的)

4.通知的行为,常规的和按照堆栈顺序的


activity的配置:

<activity            android:name="com.example.testnotification.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!-- 确保这次活动是独立的,不影响应用默认的使用范围 -->        <activity            android:name="com.example.testnotification.ResultActivity"            android:excludeFromRecents="true"            android:label="@string/title_activity_result"            android:launchMode="singleTask"            android:taskAffinity="" >        </activity>                <!-- 具有保持应用程序堆栈顺序的,通知 -->        <activity            android:name="com.example.testnotification.Result2Activity"            android:label="@string/title_activity_result2"            android:parentActivityName="com.example.testnotification.MainActivity" >            <meta-data                android:name="android.support.PARENT_ACTIVITY"                android:value="com.example.testnotification.MainActivity" />        </activity>



示例代码:

/** * 测试发送一个普通行为的通知 通知在点击后,进入resultactivity,点击返回将直接结束 *  * @param v */public void onBtn1(View v) {// 构造通知,目前已经将直接使用构造方法的方式淘汰了NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setContentTitle("jiba");builder.setContentTitle("狗日傻逼");builder.setAutoCancel(true);builder.setSmallIcon(R.drawable.ic_launcher);// 通知的活动Intent notifyIntent = new Intent(this, ResultActivity.class);// Sets the Activity to start in a new, empty tasknotifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);// Creates the PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(this, 0,notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);// Puts the PendingIntent into the notification builderbuilder.setContentIntent(contentIntent);// 最后完成通知发送NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(1, builder.build());}/** * 发送保持堆栈的通知 * @param v */public void onBtn2(View v) {Intent resultIntent = new Intent(this, Result2Activity.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);// Adds the back stackstackBuilder.addParentStack(Result2Activity.class);// Adds the Intent to the top of the stackstackBuilder.addNextIntent(resultIntent);// Gets a PendingIntent containing the entire back stackPendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setContentTitle("jiba2");builder.setContentTitle("狗日傻逼2");builder.setSmallIcon(R.drawable.ic_launcher);builder.setAutoCancel(true);builder.setTicker("oh,jibalema");builder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(10, builder.build());}



原创粉丝点击