2017-08-21 32 views
0

我有一个Service可以扫描BLE设备。 Activity应显示Service收集的一些数据。服务开始时会收到通知

A Receiver已执行,待启用Bluetooth时予以通知,以便我们知道何时启动Service

如果Service正在运行,并且打开了Activity,则它只执行bindService()。但是,如果Service未运行(因为Bluetooth已禁用),应用程序将被打开并启用Bluetooth,因为绑定进程已被跳过,所以它不会为bind

如何在启动时通知Service启动或自动绑定?

谢谢。

回答

1

您可以使用LocalBroadCastManager将broadcast从您的服务发送到您的活动。

助手注册并发送Intents广播到您的过程中的本地对象。与使用sendBroadcast(意图)发送全球广播相比,这具有许多优点:

0

您可以使用本地广播接收器从您的服务。

0

在服务中使用这些代码

intent = new Intent("my-integer"); 
    intent.putExtra("message",""+a); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 

在你的活动中使用此代码

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Extract data included in the Intent 
     String data = intent.getStringExtra("message"); 
      if (!data.equals("0")) { 
      //Do something 
      } else { 
      //Do something else 
      } 
     } 

    } 
}; 
相关问题