2017-04-05 121 views
0

我有問題,關於如何關閉應用程序,當用戶點擊通知從托盤和打開活動。 整個場景如下: 例如我的應用程序名稱是testdemo如何關閉當前打開的應用程序,當點擊推送通知

假設testdemo應用程序當前打開,用戶在應用程序中做一些工作。當時推送通知到達與testdemo應用程序有關。

在這種情況下,用戶已經打開testdemo應用程序,但當點擊通知時,它再次打開testdemo應用程序。但是我想關閉當前的應用程序而不是打開的應用程序。由於當新應用程序關閉的時候也是testdemo應用程序,但是最後一個屏幕已經被用戶點擊通知之前打開。

我發送通知如下:

private void sendNotification(String title, 
           String message, 
           String receiverUid) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("uid", receiverUid); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, ++NOTIFICATION_ID, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_alert) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(++NOTIFICATION_ID, notificationBuilder.build()); 
} 

回答

0
  1. 你可以申請無它新的實例輕鬆打開當前狀態。代碼以設置你的發射意圖 樣品:
> @SuppressLint("NewApi") 
>   public Intent launchIntent(Context ctx) { 
>     final ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 
>     Intent intent = new Intent(); 
>     boolean activated = false; 
>     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
>      List<ActivityManager.AppTask> tasks = am.getAppTasks(); 
>      for (ActivityManager.AppTask task: tasks){ 
>       intent = task.getTaskInfo().baseIntent; 
>       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
>       activated = true; 
>       break; 
>      } 
>     } else { 
>      @SuppressWarnings("deprecation") 
>      final List<RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0); 
>      String myPkgNm = ctx.getPackageName(); 
>      if (!recentTaskInfos.isEmpty()) { 
>       RecentTaskInfo recentTaskInfo; 
>       final int size = recentTaskInfos.size(); 
>       for (int i = 0; i < size; i++) { 
>        recentTaskInfo = recentTaskInfos.get(i); 
>        if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) 
> { 
>         intent = recentTaskInfo.baseIntent; 
>         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
>         activated = true; 
>        } 
>       } 
>      } 
>     } 
>     if (!activated) { 
>      intent = new Intent(this, ALaunch.class); 
>     } 
>    return intent; 
>   } 
  • 您可以將觸發傳遞意圖和關閉當前活動或應用意圖額外的數據。
  • 通過一個額外的使用:

    intent.putExtra("KEY",value);

    檢索內部Activity

    value = getIntent().getExtras().get("KEY");

    使用
  • 可以manifest文件裏面說該活動僅獲得一個實例來防止再次打開它。
  • android:launchMode=["singleTop" | "singleTask" | "singleInstance"]選擇任何首選項。 singleTop更有用,在你的情況下,恕我直言。

    +0

    Thnks的答案。你能給出例子或引用的鏈接,以獲得確切的想法。 – ashish

    +0

    @ashish,好的。 DONE – Vyacheslav

    相關問題