バックグラウンド サービスを使用して BluetoothDevice に接続するアプリケーションを作成しようとしています。
アクティビティ:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
/* ... Create Data Scruct ...*/
dataStruct = new DataStruct();
/* Start Service /*
Intent intentService = new Intent(this,NewService.class);
intentService.putExtra("Data", dataStruct);
startService(intentService);
}
サービス:
public class NewService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
dataStruct = intent.getStringExtra("Data");
...
connectBluetooth(dataStruct.getDeviceBT());
return START_NOT_STICKY;
}
public void BTReceive(){
updateDataStruct();
}
}
BT が接続され、特別な何かを受信するたびに通知が発生します。通知をクリックすると、Activity が開始されます (インテントによる) 動作しますが、Activity レイアウトを開始せずにこのアプリケーションを開始したいと考えています。サービスだけ開始して、モバイルのスタートアップで開始したいです。最後の質問です。「最後のアプリケーション リスト」からアプリケーションをスワイプすると、サービスが停止し、Bluetooth 通信が失われます。それを回避する方法はありますか?Activity を強制終了したいが、サービスは維持したい
ありがとうございました