Passing parameters to the BroadcastReceiver

对着背影说爱祢 提交于 2019-12-04 07:14:00

Your CallReceiver mCallReceiver=new CallReceiver(); instance is not used for receiving intents. Instead, Android creates new instance each time. And 0 is the default value for uninitialized integer variables.

To make sure this is what happens, assign some default value to your value field:

public class RReceiver extends BroadcastReceiver {
    public int value=5;
    //...
}

and your value will always be equal 5.

As for passing data to BroadcastReceiver, add it as extra to the Intent you are broadcasting:

//in your service
Intent broadcastedIntent=new Intent(this, CallReceiver.class);
broadcastedIntent.putExtra("VALUE", 100500);
sendBroadcast(broadcastedIntent);

And then, in your CallReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    int value=intent.getIntExtra("VALUE", 0);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!