无法将对象发送到intent LocalBroadcastManager

时间:2015-10-28 10:20:11

标签: android object android-intent

抱歉我的英文。我花了很多时间尝试将一个对象从一个Activity发送到另一个Activity。我需要使用LocalBroadcastManager。这是我的例子:

FirsActivity:

         LocalBroadcastManager.getInstance(this).registerReceiver(mDialog,
                            new IntentFilter("list"));

        //code

            private BroadcastReceiver mDialog = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    PeopleObject people=
                            (PeopleObject) getIntent().getSerializableExtra("people");
                    popupWindow(people);
                }
            };

    public void popupWindow(PeopleObject people) {
//my object PeopleObject  is null

            Toast.makeText(getApplicationContext(), people.getName(), Toast.LENGTH_SHORT).show();     
        }

SecondActivity:

public void startPopUp(PeopleObject object) {
//i check in debbug my object not null
        Intent intent = new Intent("list");
        intent.putExtra("people", object);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }

PeopleObject - implements Serializable

2 个答案:

答案 0 :(得分:0)

嘿,请参考此代码,我将了解如何将意图发送到活动 -

private void MyaCtivity() {
Intent intent = new Intent("MyDevice");
sendLocationBroadcast(intent);
}

private void sendLocationBroadcast(Intent intent){
intent.putExtra("currentprice", currentp);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

答案 1 :(得分:0)

Stupit错误,我需要得到这样的意图

private BroadcastReceiver mDialog = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    PeopleObject people=
                            (PeopleObject) intent.getSerializableExtra("people");
                    popupWindow(people);
                }
            };

使用intent.getSerializableExtra("people");,而不是getIntent().getSerializableExtra("people");