如何检查Activity是否从通知开始

时间:2013-09-01 18:13:09

标签: android

我遇到问题我的MainActivity可以通过3种方式创建: 1)标准发布App 2)来自服务 3)从通知点击。 我如何检查它何时从通知点击开始?

通知代码:

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());
}

1 个答案:

答案 0 :(得分:7)

添加

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

从通知中启动意图的代码,与其他startActivity调用相同的内容只是更改值,然后在您的活动中

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

更多请参阅此问题:How do I get extra data from intent on Android?