2013-04-26 89 views
1

我正在處理用戶可以在登錄活動中登錄的應用程序。登錄的用戶可以在儀表板活動中看到他們的評論。如果有新的評論發佈在服務器上,它會推送一個通知。一切都運行完美除了一件事情,即當用戶點擊通知時,如果儀表板活動在前面,則每次都會打開一個新的儀表板活動。如何重新打開通知中的活動點擊我android

我想要的是,如果用戶點擊通知,它將只在應用程序未運行時打開儀表板活動。否則,如果儀表板活動在前面,那麼如果用戶點擊它,它將首先關閉儀表板活動,並且它將重新打開活動頁面。 這是我寫的去儀表板活動的代碼。

Intent startActivityIntent = new Intent(this, DashboardActivity.class); 
    startActivityIntent.putExtra("NotificationMessage", service_notification); 
    startActivityIntent.putExtra("flag", 1); 
    startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent launchIntent = PendingIntent.getActivity(this, 0, 
      startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    earthquakeNotificationBuilder 
       .setContentIntent(launchIntent) 
       .setContentTitle(no_of_review + " " + "new review is there") 
       .setAutoCancel(true); 

我已經嘗試了很多東西,但沒有得到解決方案。誰能幫我?提前致謝。

回答

3

,並在你的AndroidManifest.xml中的android:launchMode="singleTask"這樣你就可以和d android:clearTaskOnLaunch="true

 <activity 
      android:name=".DashboardActivity" 
      android:launchMode="singleTask" 
      android:clearTaskOnLaunch="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
+0

我已經改變了這一點。但是,如果儀表板活動未開啓,我想要的是,如果儀表板活動已經打開,那麼如果我點擊通知,它將首先關閉它,然後重新打開它。 – Avijit 2013-04-26 06:11:57

+0

so and'android:clearTaskOnLaunch =「true' too。 – buptcoder 2013-04-26 06:15:03

+0

我應該通過意圖傳遞的標誌是什麼(startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);) – Avijit 2013-04-26 06:20:01

0

你在做什麼是你正在創建一個新的活動在用戶每次點擊的通知時間。現在您需要做的是將此FLAG_ACTIVITY_CLEAR_TOP添加到您的意圖中,並且還將啓動模式更改爲清單文件。還覆蓋OnNewIntent()方法在DashboardActivity

下面是一些代碼,以幫助您

startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

,改變你的manifest資源配置文件這樣

<activity 
    android:name=".DashboardActivity" 
    android:launchMode="singleTop"> 
</activity> 

然後覆蓋OnNewIntent方法在你的DashBoard這樣的活動

只要您的行爲存在,並通知被點擊

還研究launchmodes如果你希望你的活動,以不同的表現在不同情況下0

的OnNewIntent方法將閃光。

+0

我改變了這一點。但是如果儀表板活動打開,則什麼都不會發生。我想要的是,如果儀表板活動已經打開,那麼如果我點擊通知,它將首先關閉它,然後重新打開它。 – Avijit 2013-04-26 06:16:47

+0

@Andu現在檢查它,我已經改變了答案。它應該像你想要的那樣工作 – 2013-04-26 06:25:32

+0

我應該從onCreate方法調用onNewIntent(intent)方法嗎? – Avijit 2013-04-26 06:28:55

1

此代碼將重新打開一個活動,如果打開,否則應用程序將再次開始

 ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE); 
     List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); 
     ActivityManager.RunningTaskInfo task = tasks.get(0); // get current task 
     ComponentName rootActivity = task.baseActivity; 

     Intent notificationIntent; 
     if(rootActivity.getPackageName().equalsIgnoreCase("your package name")){ 
      //your app is open 
      // Now build an Intent that will bring this task to the front 
      notificationIntent = new Intent(); 
      notificationIntent.setComponent(rootActivity); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 

     } 
     else 
     { 
//your app is not open,start it by calling launcher activity 
      notificationIntent = new Intent(context, SplashActivity.class); 


     } 
     notificationIntent.setAction(Intent.ACTION_MAIN); 
     notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 





     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

//use this pending intent in notification.. 

看到完整的例子HERE ..

+0

其工作完美:) – NarendraJi 2015-05-13 06:34:00

相關問題