2016-08-04 40 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

看起來很有前途,但由於數據已經存儲在數據庫中,我決定只是通過密鑰。謝謝 –

相關問題