0

我有一個運行服務的應用程序。該服務發送基於時間的通知(例如在11小時發送通知,並在11:30時,它發送另一個)在android中點擊通知

當用戶單擊通知時,我必須顯示一個彈出窗口(Android中的對話框類)在應用程序中。

我的解決方案是:當用戶點擊通知並顯示對話框時,我啓動我的應用程序的MainActivity。

問題:點擊將用戶帶到應用程序的第一個屏幕。如果用戶在點擊通知(我在我的應用程序中使用查看傳呼機)時處於其他屏幕中,我希望對話框顯示在當前用戶所在的屏幕上,而不是顯示在第一個屏幕上

如何我能解決這個問題嗎?感謝您的解決方案。要做到這一點

+0

然後在'Notification'中將'Page Position'設置爲'Pending Intent',並在'mainActivity'和載入頁面上獲取這個值,就像在'View Pager'中的那個位置一樣。 –

回答

0

一種方法是在你的應用類中創建一個靜態參考電流活動(瞭解如何創建一個應用程序類的例子:http://www.intertech.com/Blog/androids-application-class/

有你的應用程序類,你應該建立一個靜態的背景下,如:

public static FragmentActivity currentActivity; 

現在,你應該在你有的每個Activity.onCreate上填充這個實例。

之後,您可以使用此靜態上下文來創建您的對話框。

問候,

0

對於對話框將顯示在同一屏幕的用戶,你介紹一個Theme.Dialog主題活動,與待處理的意圖給你通知它添加it'l最好套件上建設者。像:

// This sets the pending intent that should be fired when the user clicks the 
    // notification. Clicking the notification launches a new activity. 
     Intent intent = new Intent(this, YourDialogActivity.class); 
     //set flags according to your implementation 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY); 
//  startActivity(intent); 

      // Because clicking the notification launches a new ("special") activity, 
      // there's no need to create an artificial back stack. 
      PendingIntent resultPendingIntent = 
        PendingIntent.getActivity(
        this, 
        0, 
        intent, 
        PendingIntent.FLAG_ONE_SHOT //also check the flags here 
      ); 
      mBuilder.setContentIntent(resultPendingIntent); 

     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 
     // Builds the notification and issues it. 
     mNotifyMgr.notify(integerHere, mBuilder.build()); 

在你的清單,其主題對話框中添加你的活動:

<activity 
      android:name="com.package.YourDialogActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:theme="@android:style/Theme.Dialog" /> 

,具體實施,請參閱:Define the Notification's Action

這樣一來,你就不會調用在MainActivity您的應用程序和DialogActivity將只打開應用程序,並在最小化應用程序時最後打開的活動中顯示對話框。

+0

感謝您的解決方案,它解決了我在我的問題中提到的問題,我還需要處理以下情況:當用戶單擊通知並且應用程序尚未運行時,我必須顯示與主要活動對話在後臺(即啓動應用程序,然後顯示對話框)。你能告訴我如何處理這種情況? – user1423561

+0

不客氣,請相應答覆/接受答案。您需要檢查服務中的應用程序狀態,如果應用程序未運行,則需要啓動您的應用程序,因此只需在啓動主要活動時通過意圖傳遞標誌/值並檢查該意圖的值onCreate或onResume,因此顯示對話框..我認爲你現在的實施會幫助你。你可以把它放在if-else中 – user2450263