1. 程式人生 > >Android 通知欄(Notification)點選跳轉頁面

Android 通知欄(Notification)點選跳轉頁面

1,通知欄應用:

Notification,是一種具有全域性效果的通知,可以在系統的通知欄中顯示。當 APP 向系統發出通知時,它將先以圖示的形式顯示在通知欄中。使用者可以下拉通知欄檢視通知的詳細資訊。通知欄和抽屜式通知欄均是由系統控制,使用者可以隨時檢視。 

2,程式碼:

【1】顯示通知欄。跳轉頁面   

//[1]獲取通知的管理者

        NotificationManager barmanager = (NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);

        Notification notice;

        //[2] 獲取Notification

        Notification.Builder builder = new Notification.Builder(this).setTicker("我是測試內容")

                .setSmallIcon(R.mipmap.ic_launcher).setWhen(System.currentTimeMillis());

        //開啟另一個頁面

        Intent appIntent = new Intent(this,SplashActivity.class);

        appIntent.setAction(Intent.ACTION_MAIN);

        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        //設定啟動模式

        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);



        PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,PendingIntent.FLAG_UPDATE_CURRENT);



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

            //設定通知欄中的內容

            notice = builder.setContentIntent(contentIntent).setContentTitle("我是標題").setContentText("我是內容").build();

            notice.flags=Notification.FLAG_AUTO_CANCEL;

            barmanager.notify(10,notice);

        }

【2】取消通知ID是上面設定的

 

   barmanager.cancel(10);

【3】XML要配置開啟的Activity。singleTask模式

<activity android:name=".SplashActivity"

android:launchMode="singleTask"

android:taskAffinity=""

android:excludeFromRecents="true"

/>