Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过PendingEvent将自定义可序列化对象传递给BroadcastReceiver_Java_Android_Android Pendingintent_Android Broadcastreceiver - Fatal编程技术网

Java 如何通过PendingEvent将自定义可序列化对象传递给BroadcastReceiver

Java 如何通过PendingEvent将自定义可序列化对象传递给BroadcastReceiver,java,android,android-pendingintent,android-broadcastreceiver,Java,Android,Android Pendingintent,Android Broadcastreceiver,我正在尝试使用PendingEvent将自定义序列化对象从我的IntentService传递到BroadcastReceiver 这是我的自定义对象: Row.java public class Row implements Serializable { private String name; private String address; public Row(BluetoothDevice device) { this.name = device.

我正在尝试使用PendingEvent将自定义序列化对象从我的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();
    }
}
这是我的意向服务

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);
这是我的收音机:

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
    }
}
最恼人的是,这段代码以前在我的Nexus6p(棒棒糖6.0API23)上运行。当我把同一部手机升级到安卓7.0(牛轧糖)时,它就停止工作了。牛轧糖有什么变化导致了这个问题吗

注:


我使用带有API 23的Nexus 6P上的模拟器运行了我的代码,它运行得很好。

很可能,您遇到的问题与您看到的问题相同。我在那篇博客文章中的解释是:基本上,如果一个核心操作系统进程需要修改
Intent
extras,那么该进程最终会尝试重新创建可序列化的
对象,作为设置extras
包进行修改的一部分。该进程没有您的类,因此它会得到一个运行时异常

最恼人的是,这段代码以前在我的Nexus6p(棒棒糖6.0API23)上运行

行为会因Android版本、您如何使用
PendingEvent
、以及可能的固件/ROM而有所不同。不要假设您当前的实现在任何Android版本上都是可靠的

您唯一的选择是不要将
Serializable
直接放在
Intent
额外文件中。使用
可序列化
以外的内容(例如,嵌套的
),将
可序列化
转换为
字节[]
,等等


演示后一种方法,该方法应用于
Parcelable
对象。应适用于
可序列化的
。(请向AyeVeeKay提示评论中的链接)。

感谢您的澄清。我已经实现了一种方法,类似于对可分组对象进行编组/解编组,使用代码段@AyeVeeKay对可序列化对象进行序列化/反序列化。是的,这应该具有相同的净效果:使您的类远离没有它的进程。另一个很好的答案。谢谢@commonware