[英]How to pass parameters to Broadcast receiver class?
I have created a Broadcast receiver and it is working fine. 我创建了一个广播接收器,它工作正常。 but I need to pass a handler to that class.
但我需要将处理程序传递给该类。
public static class DataReceiver extends BroadcastReceiver {
Handler handler;
DataReceiver(Handler loghandler) {
this.handler = loghandler;
}
@Override
public void onReceive(Context context, Intent intent) {
//things goes here
}
}
Currently I am using like this & It is working if constructor override is not available. 目前我正在使用这样的&如果构造函数重写不可用,它正在工作。
Intent intent = new Intent(this, DataReceiver .class);
but I need to pass the handler too. 但是我也需要通过处理程序。 How can I send the handler?
如何发送处理程序? Thanks
谢谢
I don't really understand what you are trying to accomplish but i think this may help you. 我不太了解您要完成的工作,但我认为这可能对您有所帮助。 You don't need to make a whole new class for your broadcast receiver but you can use it inside your Main Activity like this:
您不需要为广播接收器创建一个全新的类,但可以在“主要活动”中使用它,如下所示:
BroadcastReceiver receiveLocationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Your custom action
}
};
IntentFilter receiveLocationFilter = new IntentFilter();
receiveLocationFilter.addAction("android.intent.RECEIVE_LOCATION");
Register the receiver in "onStart": 在“ onStart”中注册接收器:
registerReceiver(receiveLocationReceiver, receiveLocationFilter);
Unregister it in "onStop": 在“ onStop”中注销:
unregisterReceiver(receiveLocationReceiver);
Then when you need to send the broadcast all you need is : 然后,当您需要发送广播时,您需要做的是:
Intent sendBroadcastIntent = new Intent("android.intent.RECEIVE_LOCATION");
sendBroadcast(sendBroadcastIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.