1. 程式人生 > >android鎖屏狀態下, 新訊息喚醒螢幕,並跳轉到指定頁面

android鎖屏狀態下, 新訊息喚醒螢幕,並跳轉到指定頁面

最近在做一個專案,有個功能,就和QQ電話差不多,我這邊手機鎖屏狀態,當QQ電話過來時,立刻喚醒螢幕,並顯示QQ來電介面,我的是當裝置端有事件發生時通知app端彈出介面,實時顯示裝置端資訊。

最核心的程式碼就是新訊息的推送通知處理,這裡不過多陳述,就是app繫結服務 ,在服務裡開啟一條執行緒,實時監測 狀態,這裡我沒用第三方推送平臺,因為感覺很少的程式碼量,沒必要吧(其實我還沒用過第三方那個推送平臺)。

在這重點說下當處於鎖屏狀態下,新訊息喚醒螢幕,並顯示指定Activity 介面,因為是新訊息到來直接進入某個介面 所以沒通知欄的標題啊,內容啊,圖示啊,都不需要,直接在監測到新訊息狀態處用Intent  實現 跳轉 到 指定介面 ,

messageNotification = new  Notification.Builder(getApplication());
messageNotification.setDefaults(Notification.DEFAULT_ALL);
messageNotification.setAutoCancel(true);
noti  = messageNotification.build();
messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE
); cancelNotification(); messageNotificatioManager.notify(messageNotificationID,noti); Intent intent = new Intent(); intent.setClass(getApplicationContext(),跳轉指定Activity的.class); intent.putExtra(); intent.putExtra(); // 需要時 傳入相應的引數 intent.putExtra(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
); startActivity(intent);

在指定的Activity 的Oncreate()方法處 加入

    @Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
getWindow().addFlags(

            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|

            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|

            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.******);
 這樣 就可實現  和 QQ 電話 一樣 鎖屏狀態下   直接 彈出 指定介面的 資訊