简体   繁体   English

如何在广播接收器中调用服务方法?

[英]How to call a service method in a broadcastreceiver?

So, I have an app that starts a service. 因此,我有一个启动服务的应用程序。 This service starts to scan for bluetooth devices with BTAdapter.startDiscovery(). 此服务开始使用BTAdapter.startDiscovery()扫描蓝牙设备。 Further I have a broadcastreceiver which listens for the DISCOVERY_FINISHED action. 另外,我有一个广播接收器,它监听DISCOVERY_FINISHED动作。 If that occurs I want to call a method from onReceive() in my service that starts the scanning process again. 如果发生这种情况,我想从服务中的onReceive()中调用一个方法,以再次启动扫描过程。 How am I gonna do this? 我该怎么办?

Here my receiver: 这是我的接收者:

public class PollingReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ScanBTService sBTs = new ScanBTService();
        sBTs.startScan();
    }
}

and here the service: 这里的服务:

public class ScanBTService extends IntentService {

    private BluetoothAdapter mBTAdapter;
    private PollingReceiver mPollingReceiver;

    public ScanBTService() {
        super("ScanBTService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBTAdapter = btManager.getAdapter();
        mBTAdapter.startDiscovery();
    }

    public void startScan() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        mBTAdapter.startDiscovery();
    }
}

In your onReceive()-method, restart your service using the following two lines. 在onReceive()方法中,使用以下两行重新启动服务。 I did not tested it out but it should work like that. 我没有对其进行测试,但是它应该可以那样工作。

 @Override
 public void onReceive(Context context, Intent intent) {
     //ScanBTService sBTs = new ScanBTService();
     //sBTs.startScan();
     Intent i = new Intent(getApplicationContext(), ScanBTService.class);
     startService(i);
 }

You can then remove the startScan()-method, too. 然后,您也可以删除startScan()方法。

尝试使用以下方法解决该方法:

context.startService(new Intent(context, SimpleWakefulService.class));

Since you are using an IntentService, you will need to create an intent for the started service to handle. 由于使用的是IntentService,因此需要为启动的服务创建一个Intent来处理。

This can be achieved by the following : 这可以通过以下方法实现:

Intent intent = new Intent(context, ScanBTService.class);
startService(intent);

As described here : https://developer.android.com/training/run-background-service/send-request.html 如此处所述: https : //developer.android.com/training/run-background-service/send-request.html

Now, if you are looking to have a service that maintains bluetooth connections, discover devices, send & receive data... If this is the case, then in my experience, i would argue the following points : 现在,如果您正在寻找一种维护蓝牙连接,发现设备,发送和接收数据的服务...如果是这种情况,那么根据我的经验,我会提出以下几点意见:

Hope it helps Cheers 希望它可以帮助干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM