2016-08-04 75 views
4

我的应用程序不适用于Android 7.我的BroadcastReceiver.onReceive方法被调用,但intent.getExtras的内容缺失。我已验证数据是否正确加载。这是我的onReceive方法的一个片段,其中intent作为参数传递给onReceive。Android 7 BroadcastReceiver onReceive intent.getExtras缺少数据

Bundle bundle = intent.getExtras(); 
textMessage = bundle.getString("TEXT_MESSAGE"); 
ArrayList<MyPhoneNumber> phoneNumbersToText = bundle.getParcelableArrayList("PHONE_NUMBERS"); 

textMessage和phoneNumbersToText都为空。

下面是从我的清单文件中的一个片段:

<receiver android:process=":remote" android:name="com.friscosoftware.timelytextbase.AlarmReceiver"></receiver> 

在此处,将数据加载一个片段:

Intent intent = new Intent(context , AlarmReceiver.class); 
intent.putExtra(Constants.TEXT_MESSAGE, scheduledItem.getMessageToSend()); 
intent.putExtra(Constants.PHONE_NUMBERS, scheduledItem.getPhoneNumbersToText());  

PendingIntent sender = PendingIntent.getBroadcast(context, getRequestCodeFromKey(key), intent, PendingIntent.FLAG_UPDATE_CURRENT); 

// Get the AlarmManager service 
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, selectedDateTime.getTimeInMillis(), sender); 

相同的代码工作正常,在Android的6

有什么想法此处需要对Android 7进行哪些更改?

谢谢

回答

1

+1,看起来你和我有同样的问题。我将它记录在您评论的跟踪器(https://code.google.com/p/android/issues/detail?id=216581)上。

我的解决方案是使用SharedPreferences来存储我的自定义对象。然后,当警报管理员触发时,我运行以下操作来获取对象。 tl; dr,我使用GSON将我的自定义POJO作为字符串进入/退出SharedPrefs序列化/反序列化。例如:

String json = getSharedPrefs(context).getString(NotificationUtility.NEXT_REMINDER_KEY, "No reminder found"); 
    try { 
     Gson gson = new Gson(); 
     Reminder reminder = gson.fromJson(json, Reminder.class); 
     if (reminder != null) { 
      return reminder; 
     } 
    } catch (Exception error) { 
     Log.i(TAG, "Error parsing json: " + error.getMessage(), error); 
     return null; 
    } 
    return null; 

希望这可以帮助你!

+0

谢谢,我会试试你的建议。希望有一个解决方案即将出台。 –

+1

您的解决方案有效,但由于其他原因,我决定将数据存储在数据库中,只需传递密钥即可。 –

0

我有一个类似的问题,但我想我找到了一个简单的解决方案。把你的数据放入一个Bundle中,并发送该Bundle与你的闹钟意图。在我的情况下,我想以我的意图发送一个可序列化的对象。

设置报警:

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("exapmle", 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); 

收到报警:

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

} 

它的工作对我罚款。希望它可以帮助:)

+0

看起来很有前途,但由于数据已经存储在数据库中,我决定只是通过密钥。谢谢 –