一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRes ...
一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。
1 package com.zzw.testnotification; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.os.Bundle; 10 import android.support.v4.app.NotificationCompat.Builder; 11 import android.util.Log; 12 import android.widget.RemoteViews; 13 14 public class MainActivity extends Activity { 15 16 private static final String TAG = "---->"; 17 18 private final int NOTIFICATION_ID = 0xa01; 19 private final int REQUEST_CODE = 0xb01; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 Log.d(TAG, "onCreate"); 26 } 27 28 @Override 29 protected void onResume() { 30 Log.d(TAG, "onResume"); 31 super.onResume(); 32 } 33 34 @Override 35 protected void onDestroy() { 36 Log.d(TAG, "onDestroy"); 37 super.onDestroy(); 38 } 39 40 @Override 41 protected void onPause() { 42 Log.d(TAG, "onPause"); 43 super.onPause(); 44 } 45 46 @Override 47 protected void onRestart() { 48 Log.d(TAG, "onRestart"); 49 super.onRestart(); 50 } 51 52 @Override 53 protected void onStart() { 54 Log.d(TAG, "onStart"); 55 super.onStart(); 56 } 57 58 @Override 59 protected void onStop() { 60 super.onStop(); 61 Log.d(TAG, "onStop"); 62 sendNotification(this, NOTIFICATION_ID, "这是标题", "这是内容"); 63 } 64 65 66 //可当作发送通知栏消息模版使用 67 private void sendNotification(Context context, int notification_ID, String title, String content) { 68 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 69 70 //使用默认的通知栏布局 71 Builder builder = new Builder(context); 72 // 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏 73 builder.setSmallIcon(R.drawable.ic_launcher); 74 builder.setContentTitle(title); 75 builder.setContentText(content); 76 77 Notification notification = builder.build(); 78 79 /* 使用自定义的通知栏布局 80 * 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局 81 */ 82 // RemoteViews contentView = new RemoteViews(context.getPackageName(), 83 // R.layout.notification); 84 // contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher); 85 // contentView.setTextViewText(R.id.title, "土耳其和IS的秘密"); 86 // contentView.setTextViewText(R.id.text, "土耳其拒绝向俄罗斯道歉,怀疑有IS撑腰"); 87 // notification.contentView = contentView; 88 89 // 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。 90 // 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。 91 // 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。 92 notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS; 93 94 // 点击notification自动消失 95 notification.flags = Notification.FLAG_AUTO_CANCEL; 96 97 // 通知的时间 98 notification.when = System.currentTimeMillis(); 99 100 // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。101 Intent intent = new Intent(context, MainActivity.class);102 103 // 当用户点击通知栏的Notification时候,切换回MainActivity。104 PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);105 notification.contentIntent = pi;106 107 // 发送到手机的通知栏108 notificationManager.notify(notification_ID, notification);109 }110 111 //可当作清除通知栏消息模版使用112 private void deleteNotification(int id) {113 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);114 notificationManager.cancel(id);115 }116 }
原标题:通知栏发送消息Notification(可以使用自定义的布局)
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。