【问题标题】:How to run binded service method in AsyncTask android如何在 AsyncTask android 中运行绑定的服务方法
【发布时间】:2015-03-04 22:20:48
【问题描述】:

您好,我遇到了一个问题,我想在我的 AsyncTask 中使用 Running 服务方法。

public class SplashActivity extends Activity {

    boolean bBindedBluetooth;
    static BTService Bluetooth;

    ...

    @Override
    protected void onStart() {
      super.onStart();  
      new ActivityStarter().execute("");};@Override
    protected void onStop() { super.onStop(); unbindMyService("onStop"); };  
    @Override
    protected void onPause(){ super.onPause(); unbindMyService("onPause"); };
    @Override
    protected void onDestroy() { super.onDestroy(); unbindMyService("onDestroy"); }; 


    ...

    private class ActivityStarter extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... params) {
            try{
                Intent btIntent = new Intent(SplashActivity.this, BTService.class);
                bindService(btIntent, scConnection, Context.BIND_AUTO_CREATE);
                //Bluetooth.initService();
                //Bluetooth.StartBluetoothConnection(); 

            } catch (Exception e){
                Log.e(this.getClass().getSimpleName(), e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Intent MainActivityIntent = new Intent(SplashActivity.this, MainActivity.class);
            SplashActivity.this.startActivity(MainActivityIntent);
            SplashActivity.this.finish();
        }

    }           


    private void unbindMyService(String methodNameForLog){
        try{
              if(bBindedBluetooth){
                  unbindService(scConnection);
                  bBindedBluetooth = false;
              }
          }catch(Exception e){
              Log.e( this.getClass().getSimpleName(), methodNameForLog + " " + e.getMessage() );
          }
    }

    ServiceConnection scConnection = new ServiceConnection() {

          public void onServiceDisconnected(ComponentName name) {
           bBindedBluetooth = false;
           Bluetooth = null;
          }

          public void onServiceConnected(ComponentName cnName, IBinder ibService) {
           bBindedBluetooth = true;
           LocalBinder mLocalBinder = (LocalBinder)ibService;
           Bluetooth = mLocalBinder.getServerInstance();

          }
    };   
}

问题出在这里,我不知道如何解决这个问题。

//Bluetooth.initService();
//Bluetooth.StartBluetoothConnection();
  1. 运行 BluetoothService - 工作
  2. 绑定 BluetoothService - 有效
  3. 运行必须将我连接到 BluetoothDevice 的 AsyncTask。
  4. 在 1-3 之后运行 MainActivity。 - 作品

广告3。当我尝试运行 Bluetooth.initService LOGCAT 时显示:

执行doInBackground()时出错;

在 initService() 中我没有代码(仅用于测试)

请帮忙。

【问题讨论】:

    标签: java android android-asynctask android-service


    【解决方案1】:

    您必须将您对 Bluetooth 对象的使用转移到 onServiceConnected(),因为那是服务实际绑定的时间,这可能会在您调用 bindService() 后立即发生,也可能不会发生

    您也可以在onStart() 中绑定到AsyncTask 之外的服务,并从onServiceConnected() 启动AsyncTask

    绑定到服务无论如何都使用回调,并且不应该对 UI 线程造成负担。

    【讨论】:

    • 是的,你是对的,但是我无法检查 asyncTask 是否已建立连接。对不起我的英语;)我想从绑定的蓝牙对象调用方法
    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2011-05-25
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多