从BroadcastReceiver启动活动时,getStringExtra()返回null

时间:2015-09-20 19:33:36

标签: android broadcastreceiver

解决

当尝试从我的BroadcastReceiver启动AppCompatActivity时,在onCreate()中传递的额外参数是否为null。可能有问题吗?

(Android 5.1.1)

在BroadcastReceiver中,我正在调用我的活动:

@Override
public void onReceive(Context context, Intent intent) {
    . . .
    Intent detailsActivityIntent = new Intent(context, DetailsActivity.class);
    intent.putExtra(DetailsActivity.ARG_ACCOUNT_ID, accountId);
    detailsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(detailsActivityIntent);
}

通过这样的通知打开BroadcastReceiver:

. . .
notificationBuilder.setContentIntent(
            NotificationActionReceiver.getPendingIntent(account, getApplicationContext()));
. . .

public PendingIntent getPendingIntent(Account account, Context context) {
    Intent intent = new Intent(context, NotificationActionReceiver.class);
    intent.putExtra(DetailsActivity.ARG_ACCOUNT_ID, account.getAccountId());

    return PendingIntent.getBroadcast(context, account.getId().intValue(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

在我的onCreate中,我只是打开片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    String accountId = getIntent().getStringExtra(ARG_ACCOUNT_ID);
    // ** accountId is null **

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.details_container, DetailsFragment.newInstance(accountId))
                .commit();
    }
}

0 个答案:

没有答案