PendingIntent不会发送Intent附加内容

时间:2013-08-03 22:38:26

标签: android android-intent android-service android-pendingintent

我的MainActicityRefreshService Intent开始booleanisNextWeek名为RefreshService

我的Notification生成一个MainActivity,当用户点击它时会启动 Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek)); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek); Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false))); pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent); notification = builder.build(); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(NOTIFICATION_REFRESH, notification);

这看起来像这样:

notificationIntent

正如您所看到的,boolean应该IS_NEXT_WEEK额外的isNextWeek PendingIntent,其值Notification

当我现在点击此false时,我始终将isNextWeek作为MainActivity

的值

这是我在 isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false); 中获取值的方式:

08-04 00:19:32.500  13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity sent: isNextWeek: true
08-04 00:19:32.510  13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService got: isNextWeek: true
08-04 00:19:32.510  13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService put in Intent: isNextWeek: true
08-04 00:19:41.990  13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity.onCreate got: isNextWeek: false

日志:

MainActivity

当我直接使用Intent使用“NesxtxtValue”启动 Intent i = new Intent(this, MainActivity.class); i.putExtra(IS_NEXT_WEEK, isNextWeek); finish(); startActivity(i); 时,这样:

true

一切正常,当isNextWeektrue时我得到false

如果总是有PendingIntent.getActivity(...)值,我该怎么办?

更新

这解决了这个问题: https://stackoverflow.com/a/18049676/2180161

引用:

  

我怀疑是,因为意图中唯一改变的是   额外的,PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 工厂方法是   只需将旧意图重新用作优化。

     

在RefreshService中,尝试:

PendingIntent.FLAG_UPDATE_CURRENT
     

请参阅:

     

http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT

更新2

请参阅answer below为什么最好使用{{1}}。

3 个答案:

答案 0 :(得分:19)

我认为您需要通过覆盖活动中的Intent来更新onNewIntent(Intent)。将以下内容添加到您的活动中:

@Override
public void onNewIntent(Intent newIntent) {
    this.setIntent(newIntent);

    // Now getIntent() returns the updated Intent
    isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);        
}

修改

只有在收到意图时您的活动已经启动时才需要这样做。如果您的活动是由意图启动(而不仅仅是恢复),则问题出在其他地方,我的建议可能无法解决。

答案 1 :(得分:19)

由于内存使用效率低,使用PendingIntent.FLAG_CANCEL_CURRENT不是一个好的解决方案。而是使用 PendingIntent.FLAG_UPDATE_CURRENT

还可以使用 Intent.FLAG_ACTIVITY_SINGLE_TOP (如果活动已经在历史堆栈的顶部运行,则不会启动该活动。)

Intent resultIntent = new Intent(this, FragmentPagerSupportActivity.class).
                    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
resultIntent.putExtra(FragmentPagerSupportActivity.PAGE_NUMBER_KEY, pageNumber);

PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

然后:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);

            int startPageNumber;
            if ( savedInstanceState != null)
            {
                startPageNumber = savedInstanceState.getInt(PAGE_NUMBER_KEY);
//so on

现在应该可以了。

如果仍然没有预期的行为,请尝试实现void onNewIntent(Intent intent)事件处理程序,这样就可以访问为活动调用的新意图(这与调用getIntent()不同,这个将始终返回启动您活动的第一个Intent。

@Override
protected void onNewIntent(Intent intent) {
    int startPageNumber;

    if (intent != null) {
        startPageNumber = intent.getExtras().getInt(PAGE_NUMBER_KEY);
    } else {
        startPageNumber = 0;
    }
}

答案 2 :(得分:3)

以下代码应该有效: -

int icon = R.drawable.icon;
String message = "hello";
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("isNexWeek", true);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, pIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

在MainActivity onCreate:

if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isNextWeek")) {
        boolean isNextWeek = getIntent().getExtras().getBoolean("isNextWeek");
}