7

有誰知道是否有任何與Android 6.0(棒棒糖)相比Android 7.0(Nougat)處理意圖演員的更改嗎?Android 7意向演員失蹤

長話短說:我的應用程序在4.1(16)到6.0(23)的所有版本上按預期工作,但在android 7.0(24)上崩潰!

該應用程序創建一個意圖定製廣播接收器,有額外的意圖。但是,在android 7上,廣播接收器收到的意圖中不存在額外信息。

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

堆棧跟蹤顯然給出了NullPointerException異常是飛機失事的原因。 如果它會在所有版本中崩潰,那麼它不會太奇怪,但在這種情況下,它只有最新的android。有沒有人有任何想法?

注意:我試圖創建包含不同標誌的待定意圖,包括(0PendingIntent.FLAG_UPDATE_CURRENTPendingIntent.FLAG_CANCEL_CURRENT)仍然得到完全相同的結果。

+2

我不能用你的問題代碼重現你的問題,所以我猜測可能是真正的代碼不符合問題的代碼。在你真實的代碼中,是否有你自定義的'Parcelable'類的形式? – CommonsWare

+0

是的,除了三個測試字符串額外功能外,我還在同一個意圖中存儲了一個Parcelable對象。當然,它也從接收者收到的意圖以及樹字符串額外缺失。 – Konaras

+1

24不允許傳入一個Parcelable對象到AlarmManager中,http://stackoverflow.com/questions/38466080/dp5-7-0-does-adding-extras-to-a-pending-intent-fail –

回答

14

將自定義Parcelable置於PendingIntent從未特別可靠,it flat-out will not work in an AlarmManagerPendingIntent on Android 7.0。其他流程可能需要將值填寫到Intent中,並且涉及操作額外部分,並且這不能在任何過程中完成,除了您自己的過程,因爲沒有其他過程具有您自定義的Parcelable類。

This SO answer有一種解決方法,將Parcelable自己轉換爲/從byte[]轉換爲/從byte[]

+0

可以爲此提供一些代碼。 –

+0

與可序列化相同 – Lemberg

2

我有類似的問題,但我想我找到了一個簡單的解決方案。將你的數據放在一個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"); 
    } 

} 

它的工作對我罰款。希望它有助於:)