2013-09-01 41 views
8

開始我有問題我的MainActivity可以通過3種方式創建:從通知點擊來自服務 1)非標準啓動應用 2))。 如何檢查通知點擊開始的時間?如何檢查活動從通知

通知碼:

private void createNotification() 
{ 
    Log.d("service createNotification",MainActivity.TAG); 
    Context context = getApplicationContext(); 
    Intent notificationIntent = new Intent(this,MainActivity.class); 
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentTitle(this.getString(R.string.notification_title)) 
      .setContentText(this.getString(R.string.notification_text))    
      .setContentIntent(pendingIntent)    
      .setSmallIcon(R.drawable.ic_launcher); 

    getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification()); 
} 

回答

8

添加

intent.putExtra("started_from","notification"); 

到開始從通知的意圖的代碼,同樣的事情在其他startActivity調用只是改變了值,然後裏面你活動

String startedFrom = getIntent().getStringExtra("started_from"); 

欲瞭解更多請參考此問題:How do I get extra data from intent on Android?

+2

這種方法並不奏效,它總是在3種情況下得到通知。 – Abbath

+0

您需要將putExtra中的「通知」更改爲其他內容,以「服務」爲例。 –

+0

完全不起作用,請看@http://stackoverflow.com/questions/14122085/determine-if-activity-is-called-by-a-notification if if any1 interested –