将不同的int值传递给BroadcastReceiver

时间:2013-09-14 11:46:13

标签: android broadcastreceiver conditional-statements

我正在开发一个需要将int值传递给BroadcastReceiver的应用。

  1. 如何向BroadcastReceiver发送值?
  2. int值根据条件而变化。如何根据条件发送存储的值?
  3. 这是我的代码:

    switch(item) {
        case 0:
            pro = 0;
            levelDialog.dismiss();
            break;
        case 1:
            pro = 1;      
            levelDialog.dismiss();
            break;
        case 2:
            pro = 2;
            levelDialog.dismiss();
            break;
    }
    

    专业价值根据条件变化。如何将此值发送至BroadcasteReceiver,如何在BroadcastReceiver中收到?

1 个答案:

答案 0 :(得分:0)

首先,您需要注册BroadcastReceiver。发送广播时,需要将int值存储到Intent中,并将该Intent传递给BroadcastReceiver。

您可以尝试这样的事情:

Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);

以下是BroadcastReceiver的示例代码:

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
 int pro = intent..getIntExtra("pro", 0);
 Intent service = new Intent(context, WordService.class);
 context.startService(service);
}
}