Android-通知欄Notification学習

3000 ワード

https://blog.csdn.net/vipzjyno1/article/details/25248021
一、使用手順
1)通知欄のBuilder構築クラス(Create a Notification Builder)を作成する
2)通知欄のAction(Define the Notification’s Action)を定義する
通知バーのアクションの定義(Define the Notification’s Action)
3)通知欄クリックイベントの設定(Set the Notification’s Click Behavior)
通知欄クリックイベントの設定(Set the Notification’s Click Behavior)
4)通知(Issue the Notification)
二、コード実装
1)取得状態通知欄管理
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2)通知バーコンストラクタのインスタンス化
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

3)builderの設定
mBuilder.setContentTitle("    ")//       
	.setContentText("    ") //         
	.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //         
//	.setNumber(number) //         
	.setTicker("      ") //          ,        
	.setWhen(System.currentTimeMillis())//       ,         ,           
	.setPriority(Notification.PRIORITY_DEFAULT) //        
//	.setAutoCancel(true)//                          
	.setOngoing(false)//ture,             。               ,      (     )          ,      (       ,    ,      )
	.setDefaults(Notification.DEFAULT_VIBRATE)//       、           、                  ,  defaults  ,    
	//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND      // requires VIBRATE permission
	.setSmallIcon(R.drawable.ic_launcher);//     ICON

4)通知欄PendingIntentの設定(クリックアクションイベント等)
  • 属性
  • FLAG_ONE_SHOT        PendingIntent      ,        
    
    FLAG_NO_CREATE            PendingIntent   ,       PendingIntent,    NULL
    
    FLAG_CANCEL_CURRENT           PendingIntent    ,     ,      PendingIntent,             ,             
    
    FLAG_UPDATE_CURRENT          PendingIntent
    
  • さまざまな状況では、contentIntent:通知ウィンドウ領域で、Notificationがクリックされたときの応答イベントがintentによってトリガーされます.deleteIntent:ユーザーがすべてのクリアボタンをクリックすると、イベントをクリアすべきIntentが鳴ります.fullScreenIntent:非常事態に応答するフルスクリーンイベント(例えば、着信イベント)、すなわち、通知が来た場合、通知領域で通知をクリックするステップをスキップしてfullScreenIntent代表のイベントを直接実行する.
    Intent intent = new Intent(context,XXX.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    mBuilder.setContentIntent(pendingIntent)
    

    5)通知を出す
    mNotificationManager.notify(notifyId, mBuilder.build());
    

    三、カスタム通知欄
    https://juejin.im/entry/578ef709c4c971005e0b3251
    ここではRemoteViewsというクラスを使います.注意RemoteViewクラスは、カスタムビューレイアウトファイルでは、FrameLayout、LinearLayout、RelativeLayoutの3種類のレイアウトコントロールとAnalylogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView、AdapterViewFlipperの表示コントロールのみをサポートしています.これらのクラスのサブクラスまたはAndroidが提供する他のコントロールはサポートされていません.そうでないとClassNotFoundException例外が発生します
    手順は次のとおりです.
    1)カスタムビューの作成
    2)リモートビューオブジェクトの取得(注意:NotificationのcontentViewは空にできません)
    3)PendingIntentを設定(各種イベントに対応)
    4)Notificationの開始