我正在开发一个需要将int值传递给BroadcastReceiver
的应用。
BroadcastReceiver
发送值?这是我的代码:
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
中收到?
答案 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);
}
}