0

這是問題所在。我的程序在Android 6.0中運行完美。將設備更新至android 7.0後。 Pendingintent無法將可分發的數據傳遞給boradcast reveiver。這是代碼。Pendingintent getbroadcast丟失可解釋數據

火災報警

public static void setAlarm(@NonNull Context context, @NonNull Todo todo) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    intent.putExtra("KEY_TODO", todo); 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, todo.remindDate.getTime(), alarmIntent); 
} 

的Todo是Parcelable類,而待辦事項是我需要通知的實例。

在BroadcastReceiver中,我無法獲取可壓縮數據。

public void onReceive(Context context, Intent intent) { 

    Todo todo = intent.getParcelableExtra("KEY_TODO"); 

} 

這裏是意圖的結果,當我調試 enter image description here

我不知道爲什麼意圖僅包含一個整數,我從來沒有把它放在哪裏是Parcelable待辦事項。 此代碼在安卓6.0沒有問題,但不能在7.0

+0

您是否嘗試過將它添加到「額外」之前包裹你的'Todo'對象的'Bundle'?這通常適用於傳遞自定義'Parcelable'對象到'AlarmManager'(但現在可能在Android 7中被破壞)。我會對你的發現感興趣。 –

+0

要添加額外的:'Bundle bundle = new Bundle; bundle.putParcelable(「todo」,todo); intent.putExtra(「KEY_TODO」,bundle);'。提取額外的:Bundle bundle = intent.getBundleExtra(「KEY_TODO」); if(bundle!= null){Todo todo = bundle.getParcelableExtra(「todo」); }' –

回答

9

報價myself運行:

定製Parcelable類—那些專屬於您的應用程序,而不是一個—有Android框架的一部分 用作Intent附加費時的間歇性問題。基本上,如果核心操作系統進程 需要修改Intent附加組件,那麼該過程會結束嘗試 以重新創建Parcelable對象,作爲設置 附加組件Bundle以進行修改的一部分。該過程沒有你的 類,所以它得到一個運行時異常。

這可能發生的一個領域是AlarmManager。使用 的代碼自定義Parcelable對象AlarmManager可能在舊版Android will not work on Android N上工作 。

最有效的解決方法,我知道的是自己手動轉換Parceablebyte[]並放進了Intent額外的手動轉換回一個Parcelable需要。 This Stack Overflow answer 顯示該技術,而this sample project提供了一個完整的工作示例。

關鍵位是Parcelablebyte[]之間的轉換:

/*** 
Copyright (c) 2016 CommonsWare, LLC 
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. You may obtain a copy 
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
by applicable law or agreed to in writing, software distributed under the 
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied. See the License for the specific 
language governing permissions and limitations under the License. 

From _The Busy Coder's Guide to Android Development_ 
https://commonsware.com/Android 
*/ 

package com.commonsware.android.parcelable.marshall; 

import android.os.Parcel; 
import android.os.Parcelable; 

// inspired by https://stackoverflow.com/a/18000094/115145 

public class Parcelables { 
    public static byte[] toByteArray(Parcelable parcelable) { 
    Parcel parcel=Parcel.obtain(); 

    parcelable.writeToParcel(parcel, 0); 

    byte[] result=parcel.marshall(); 

    parcel.recycle(); 

    return(result); 
    } 

    public static <T> T toParcelable(byte[] bytes, 
            Parcelable.Creator<T> creator) { 
    Parcel parcel=Parcel.obtain(); 

    parcel.unmarshall(bytes, 0, bytes.length); 
    parcel.setDataPosition(0); 

    T result=creator.createFromParcel(parcel); 

    parcel.recycle(); 

    return(result); 
    } 
} 
+0

你救我一天!!!!!!!!!!!!!!!!!!!!!!!!!!!!!這是Android N系統,我無法通過AlarmManager傳遞可以分類的數據。 – BIN