0

我創建了兩個應用程序。使用通知將數據傳遞給應用程序

一種應用是消息接收器(APP1)和另一個應用程序(APP2)在做基於所述消息的其它任務。

第一個應用程序(app1)收到消息,創建通知並顯示在頂部。 當用戶單擊通知時,它會調用另一個應用程序(app2)根據消息執行其他任務。

如果應用程序(app2)未運行,應該啓動它。如果它已經在運行,應該顯示實例並完成任務。

我使用下面的代碼:

protected void displayNotification() { 

     Notification notification = new Notification(icon, tickerText, when); 
     Bundle xtra = new Bundle(); 

     Intent ntent = new Intent(Intent.ACTION_SEND); 
     ntent.setClassName("com.example.mytestapp", 
       "com.example.mytestapp.MainActivity"); 

     xtra.putString("id", "8610B0DD"); 
     xtra.putParcelable("message", msg); 

     ntent.putExtras(xtra); 
     ntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     ntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       ntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     notification.setLatestEventInfo(context, contentTitle, contentText, 
       pendingIntent); 
     final int button_Click = 1; 
     nm.notify(button_Click, notification); 
} 

這工作不錯,但它創建多個實例另一個應用程序(應用)的

有什麼辦法可以防止創建這個多個副本?

回答

0

與此代碼完美結合。

  Intent ntent = new Intent(); 
      ntent.setClassName("com.project.test", 
        "com.project.test.MainActivity"); 
      ntent.setType("vnd.android-dir/mms-sms"); 

      ntent.putExtras(bundle); 

      int flags = Intent.FLAG_ACTIVITY_NEW_TASK 
        | Intent.FLAG_ACTIVITY_SINGLE_TOP 
        | Intent.FLAG_ACTIVITY_CLEAR_TOP; 
      ntent.setFlags(flags); 

      //startActivity(ntent); 
相關問題