android intent { (has extras) },Android 7 intent extras missing

问题

Does anyone know if there are any changes to how Android 7.0 (Nougat) handles intent extras compared to Android 6.0 (Lollipop)?

Long story short: my app works as intended on all versions from 4.1(16) to 6.0(23) but crashes on android 7.0(24)!

The app creates a pending intent with an intent to a custom broadcast receiver which has extras. However, on android 7 none of the extras are present in the intent received by the broadcast receiver.

MainActivity.java

Intent intent = new Intent(context, PollServerReceiver.class);

// TODO: Remove after DEBUGGING is completed!

intent.putExtra("TESTING1", "testing1");

intent.putExtra("TESTING2", "testing2");

intent.putExtra("TESTING3", "testing3");

// PendingIntent to be triggered when the alarm goes off.

final PendingIntent pIntent = PendingIntent.getBroadcast(context,

PollServerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// Setup alarm to schedule our service runs.

AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstRun, freqMilis, pIntent);

PollServerReceiver.java

Bundle extras = intent.getExtras();

Log.d(TAG, "onReceive: TESTING1 = " + extras.getString("TESTING1")); // null here

// None of the three "TESTING*" keys are there!

for (String key : extras.keySet()) {

Object value = extras.get(key);

Log.d(TAG, String.format("onReceive extra keys: %s %s (%s)", key, value.toString(), value.getClass().getName()));

}

Stack trace obviously gives the NullPointerException as the cause of crash.

It would not be so weird if it would crash among all versions, but in this case its the latest android only. Has anyone got any ideas please?

Note: I have tried creating pending intents with different flags including (0, PendingIntent.FLAG_UPDATE_CURRENT, PendingIntent.FLAG_CANCEL_CURRENT) still got the exact same result.

回答1:

Putting a custom Parcelable in a PendingIntent has never been especially reliable, and it flat-out will not work in an AlarmManager PendingIntent on Android 7.0. Other processes may need to fill in values into the Intent, and that involves manipulating the extras, and that can't be done in any process but your own, since no other process has your custom Parcelable class.

This SO answer has a workaround, in the form of converting the Parcelable yourself to/from a byte[].

回答2:

I had a similar problem but I think I found an easy solution. Put your data inside a Bundle and send that Bundle with your intent. In my case I wanted to send a serializable object with my intent.

Setup the alarm:

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, AlarmReciever.class);

Bundle bundle = new Bundle();

//creating an example object

ExampleClass exampleObject = new ExampleClass();

//put the object inside the Bundle

bundle.putSerializable("example", exampleObject);

//put the Bundle inside the intent

intent.putExtra("bundle",bundle);

PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//setup the alarm

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

Receive the alarm:

public class AlarmReciever extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// get the Bundle

Bundle bundle = intent.getBundleExtra("bundle");

// get the object

ExampleClass exampleObject = (ExampleClass)bundle.getSerializable("example");

}

}

It worked fine for me. Hope it helps :)

来源:https://stackoverflow.com/questions/39573849/android-7-intent-extras-missing

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值