簡體   English   中英

如何通過PendingIntent將自定義Serializable對象傳遞給BroadcastReceiver

[英]How to pass custom Serializable object to BroadcastReceiver via PendingIntent

我試圖使用PendingIntent將自定義的Serialized對象從我的IntentService傳遞給BroadcastReceiver。

這是我的自定義對象:

Row.java

public class Row implements Serializable {
    private String name;
    private String address;

    public Row(BluetoothDevice device) {
        this.name = device.getName();
        this.address = device.getAddress();
    }
}

這是我的IntentService

MyIntentService.java

public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        AlarmManager alarmMgr;
        PendingIntent alarmPendingIntent;
        Intent alarmIntent;
        // The object "RowsList" is passed from my MainActivity and is correctly received by my IntentService.
        // NO PROBLEMS HERE
        Row[] arrRows = (Row[])workIntent.getSerializableExtra("RowsList");

        Log.i(TAG, "Inside Intent Service...");

        int interval = 2;
        try{
            if(interval != 0) {
                alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarmIntent = new Intent(this, AlarmReceiver.class);
                alarmIntent.putExtra("IntentReason", "Reason"); // THIS GETS PASSED
                alarmIntent.putExtra("RowsList", arrRows); // THIS DOES NOT GET PASSED
                alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (interval * 1000), alarmPendingIntent);
                }
        }
        catch (Exception ignored){}
    }
}

MainActivity.java

// NO PROBLEMS HERE
private Intent myIntent;
myIntent = new Intent(getApplicationContext(), MyIntentService.class);
Log.i(TAG, "Starting Intent Service...");

myIntent.putExtra("RowsList", arrRows);
getApplicationContext().startService(intentListenBT);

這是我的BroadcastReceiver:

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    Row[] arrRows;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        String str = intent.getStringExtra("IntentReason"); // RECEIVED AS "Reason"
        arrRows = (Row[])intent.getSerializableExtra("RowsList"); // HERE IT IS null
    }
}

最令人討厭的部分是此代碼之前在我的Nexus 6P(Lollipop 6.0 API23)上運行。 一旦我將同一部手機更新到Android 7.0(Nougat),它就停止了工作。 牛軋糖中是否有任何改變導致了這個問題?

注意:

我使用帶有API 23的Nexus 6P上的模擬器運行我的代碼,它工作正常。

最有可能的是,您遇到了與自定義Parcelable實現相同的問題。 從博客中轉述自己:基本上,如果一個核心操作系統進程需要修改Intent群眾演員,這個過程纏試圖重新您的Serializable對象作為建立額外的一部分Bundle進行修改。 該進程沒有您的類,因此它獲得運行時異常。

最令人討厭的部分是此代碼之前在我的Nexus 6P(Lollipop 6.0 API23)上運行。

Android版本,您使用PendingIntent以及固件/ ROM可能會有所不同。 不要認為您當前的實現在任何Android版本上都是可靠的。

您唯一的選擇是不將Serializable直接放入Intent extra中。 使用Serializable以外的東西(例如,嵌套的Bundle ),將Serializable轉換為byte[]等。

此示例應用程序演示了后一種方法,應用於Parcelable對象。 相同的基本技術應該適用於Serializable (給評論中的鏈接提示AyeVeeKay)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM