【发布时间】: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();
- 运行 BluetoothService - 工作
- 绑定 BluetoothService - 有效
- 运行必须将我连接到 BluetoothDevice 的 AsyncTask。
- 在 1-3 之后运行 MainActivity。 - 作品
广告3。当我尝试运行 Bluetooth.initService LOGCAT 时显示:
执行doInBackground()时出错;
在 initService() 中我没有代码(仅用于测试)
请帮忙。
【问题讨论】:
标签: java android android-asynctask android-service