在活动中注册BroadcastReceiver

时间:2015-07-08 13:44:18

标签: android broadcastreceiver

我有checkboxlist,用户可以在其中选择一些Interger项目。之后,alarmManger开始向IntentService级别开火。正在向服务器发送请求,然后我得到了响应。接下来,我从响应中获取数据,然后我想要实现的是将数据发送到Map活动,而不会停止在MainActivity中触发alarmManager。

我之前尝试过将意图直接传递给Map活动但是alarmManger停止了。因此,我想将一个广播Intent与IntentService中的数据一起发送到Map,随后Map Activity可以注册BroadcastRecevier来监听这个但我在Map活动中注册data_receiver时遇到问题。在这种情况下,如何在data_receiver活动中注册Map

GetLLRD(IntentService)类中的

我有以下内容。

protected void onHandleIntent(Intent intent) {
                try {
                    //here I am getting the data from the server json response.
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<ItemDTO>>() {
                    }.getType();
                    data = gson.fromJson(sb.toString(), listType);

                    if (data != null && !data.isEmpty()) {
                        final ArrayList<ItemDTO> test = data;

                        final BroadcastReceiver data_receiver = new BroadcastReceiver() {
                            @Override
                            public void onReceive(Context context,
                                    Intent intent) {
                                Intent intent1 = new Intent(GetLLRD.this,
                                        Map.class);
                                intent1.putExtra("list_data", test);
                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent1);

                            }

                        };

                    } else {
                        Log.e("123",
                                "Avoiding null pointer, the dat is null in the GETLLRD class!!!");
                    }

                } 
}

地图活动:

public class Map扩展了ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_main);

    IntentFilter data_action= new IntentFilter();
    // Which action have I to add here?
    data_action.addAction();
    // And how to define data_receiver here since I am getting: data_receiver cannot be resolved to a variable
    registerReceiver(data_receiver, data_action);

    Intent intent = getIntent();        
    @SuppressWarnings("unchecked")
     ArrayList<ItemDTO> list =(ArrayList<ItemDTO>) intent.getSerializableExtra("list_data");

        }
}       

}

0 个答案:

没有答案