android通知(Notification)的使用
2013-11-26 15:15
375 查看
需要注意的几点:
1.通知的类型,一般普通的消息事件和正在进行的事件
2.通知的布局是可以自定义的
3.通知在android4.1以后,还支持一种detail的通知,(就是可扩展的)
4.通知的行为,常规的和按照堆栈顺序的
activity的配置:
示例代码:
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 task notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Creates the PendingIntent PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Puts the PendingIntent into the notification builder builder.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 stack stackBuilder.addParentStack(Result2Activity.class); // Adds the Intent to the top of the stack stackBuilder.addNextIntent(resultIntent); // Gets a PendingIntent containing the entire back stack PendingIntent 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()); }
相关文章推荐
- Android 通知栏发送消息Notification(可以使用自定义的布局)
- Android中使用Notification实现普通通知栏(Notification示例一)
- 使用 Android 自定义通知Notification时遇到的问题
- Android 通知栏 NotificationManager和Notification的使用总结
- Android中使用通知Notification
- android Notification 的使用(锁定通知栏)
- Android中Notification 通知的使用
- Android使用Notification实现普通通知栏(一)
- Notification,Android中通知的使用
- Android中使用Notification实现宽视图通知栏(Notification示例二)
- 【Android】使用Notification实现状态通知栏的通知
- Android Notification 通知的使用
- android使用Notification进行通知时有时候通知栏中的项背景是灰白的的
- Android NotificationManager与Notification(通知栏) 的使用
- Android下Notification(通知栏)的使用
- android通知栏之Notification的使用
- Android中通知的使用-----Notification详解
- Android Notification使用系统通知栏布局出现的图标问题
- Android中使用Notification实现状态栏的通知
- Android28--Android之Android的通知(Notification)使用详解