1

我試圖從服務發送通知,併成功執行該操作(通知確實在狀態欄上顯示)。 但是,點擊通知後,爲了觀看活動 - 我要回到手機主屏幕。另外,我在調試模式中注意到我沒有達到活動的「onCreate」方法。服務通知有效,但活動未啓動

這裏是我的一段代碼: 服務代碼:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    int icon = R.drawable.notificationicon; 
    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, NotificationsActivity.class), 0); 
    notification.setLatestEventInfo(this, "My notification","Hello World!", pendingIntent); 
    notificationManager.notify(1, notification); 

活動代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(dialogLayout); 
} 
+1

AndroidManifest.xml中的NotificationActivity類是否定義良好? (你可以粘貼你的AndroidManifest?) –

回答

0

在你從一個接收器/服務中要startActivity情況下,你必須創建一個意圖,添加一個標誌通知它開始一個新的任務,然後啓動該活動。

適應此僞代碼:

Intent startServiceIntent = new Intent(context, com.myCorp.myApp.myClass.class); 
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(startServiceIntent); 

我用這個在BootReceiver啓動的應用程序(for example use this BootReceiver)。