2014-12-23 31 views
1

我有一個服務將顯示通知。通知顯示正確,但是當我點擊nofitification時沒有任何反應。它應該在我的應用程序中打開一個活動。這裏是我的代碼:PendingIntent不起作用

public class ServiceCheckExpensesRecurring extends Service{ 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      int mNotificationId = 001; 
      Intent myIntent = new Intent(this, MenuDashboard.class); 
      myIntent.setAction(Intent.ACTION_VIEW); 
      myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
      PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      NotificationCompat.Builder n = new NotificationCompat.Builder(this) 
        .setContentTitle("New mail from " + "[email protected]") 
        .setContentText("Subject") 
        .setSmallIcon(R.drawable.common_signin_btn_icon_light) 
        .setContentIntent(pIntent) 
        .setAutoCancel(true); 

      NotificationManager notificationManager = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

      notificationManager.notify(mNotificationId, n.build()); 
      return Service.START_NOT_STICKY; 
     } 
    } 

僅供參考,MenuDashboard是一個片段(如果有幫助)。我正在使用我的索尼體驗迷你ICS

+0

嘗試此http://stackoverflow.com/a/21204851/301584但仍然不起作用 – imin

回答

3

意向可以啓動活動(或服務),但不是片段。如果MenuDashboard是片段,那麼它將不起作用。

您應該使用一個Activity來代替它,並在其中嵌入Fragment。

+0

ouch。從來不知道這一點。 android應該至少拋出一個錯誤。所以沒有辦法從通知中加載一個片段? – imin

+0

呃如何在活動中嵌入片段?我需要啓動這個片段的原因是因爲我的應用使用了導航抽屜,並且菜單內的所有活動都是片段 – imin

+0

@imin在這種情況下,您可以將Intent重新傳遞給您的活動(具有抽屜的活動)然後實現'onNewIntent()'。 – matiash