Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将参数传递给广播接收器类?_Java_Android_Broadcastreceiver_Handler - Fatal编程技术网

Java 如何将参数传递给广播接收器类?

Java 如何将参数传递给广播接收器类?,java,android,broadcastreceiver,handler,Java,Android,Broadcastreceiver,Handler,我已经创建了一个广播接收器,它工作得很好。但我需要向该类传递一个处理程序 public static class DataReceiver extends BroadcastReceiver { Handler handler; DataReceiver(Handler loghandler) { this.handler = loghandler; } @Override pub

我已经创建了一个广播接收器,它工作得很好。但我需要向该类传递一个处理程序

 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
        }
}
目前我使用的是这样的&如果构造函数重写不可用,它就可以工作

Intent intent = new Intent(this, DataReceiver .class);

但我也需要通过处理程序。如何发送处理程序?谢谢

我真的不明白你想要完成什么,但我认为这可能会对你有所帮助。您不需要为您的广播接收器创建一个全新的类,但您可以在主要活动中使用它,如下所示:

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");
在onStart中注册接收器:

registerReceiver(receiveLocationReceiver, receiveLocationFilter);
在onStop中注销它:

unregisterReceiver(receiveLocationReceiver);
然后,当您需要发送广播时,您只需要:

Intent sendBroadcastIntent = new Intent("android.intent.RECEIVE_LOCATION");
sendBroadcast(sendBroadcastIntent);

但是我也需要传递处理程序为什么?为了维护现有数据流\n然后更改它。使用LocalBroadcastManager此BroadcastReceiver首先存在于何处?为什么要使用Handler?您不能以额外的意图传递处理程序,因此您的方法无论如何都需要重写。