私はAndroid開発とStackOverflowの両方にかなり慣れていないので、以前に尋ねられた愚かな質問をしていないことを願っていますが、何も見つかりませんでした.
Bluetooth デバイスに接続するときに (最初はバインドされていない) サービスを開始し、切断するように指示したとき、または接続が失われたときにのみ停止するアプリを作成しています。サービスが開始されると、メイン アクティビティがサービスにバインドされ、onDestroy() が呼び出されるとバインドが解除されます。また、onStart が呼び出され、mIsBound が true のときに再バインドします。mIsBound Boolean は onRestoreInstanceState() で保存されます。
*mIsConnected は、mIsBound よりも適切な名前かもしれませんが、おわかりいただけたでしょうか。
マルチタスクメニューまたはサービスアイコンから、通常どおりアプリを再度開いているとき、mIsBound はまだ正しい値に設定されています。画面の向きは問題ではなく、セカンダリ アクティビティを開いて [戻る] ボタンでメイン アクティビティに戻るときも問題なく動作します。しかし、セカンダリ アクティビティでアップ ボタンを使用すると、mIsBound 値が失われ、onRestoreInstanceState() が呼び出されません。
サービスが既に実行されているかどうかを判断するには、これが必要です。サービスが実行されていない場合に bindService() を呼び出すと、それを必要とせずに開始され、停止してもまだバインドされているときにエラーが発生するためです。
サービス:
public class BluetoothService extends Service {
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
BluetoothService getService() {
return BluetoothService.this;
}
}
private void connectionLost() {
Log.e(TAG, "connection lost");
disconnect();
}
public synchronized void disconnect() {
Log.d(TAG, "disconnect");
stopSelf();
}
private void showNotification(String s) {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, s,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LedAndIrControlActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, s, s, contentIntent);
// Send the notification.
startForeground(NOTIFICATION, notification);
}
主な活動:
public class MainActivity extends Activity {
private static boolean mIsBound = false;
private BluetoothService Com;
@Override
public void onStart() {
super.onStart();
if(mIsBound){
doBindService();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
outState.putBoolean("mIsBound", mIsBound);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
mIsBound = savedInstanceState.getBoolean("mIsBound");
if(mIsBound){
doBindService();
}
}
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
doUnbindService();
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case android.R.id.home:
return true;
case R.id.item1:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
startService(new Intent(this, BluetoothService.class));
doBindService();
return true;
case R.id.item2:
// Disconnect device
Com.disconnect();
doUnbindService();
return true;
case R.id.item3:
serverIntent = new Intent(this, SecondaryActivity.class);
//serverIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(serverIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
void doBindService() {
Log.d(TAG, "doBindService");
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
bindService(new Intent(getBaseContext(), //Binding.this
BluetoothService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
Log.d(TAG, "doUnbindService");
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
Com = null;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
Com = ((BluetoothService.LocalBinder)service).getService();
invalidateOptionsMenu();
Log.i(TAG, "Service connected");
// Tell the user about this for our demo.
Toast.makeText(getBaseContext(), "local_service_connected",//Binding.this
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
Com = null;
Toast.makeText(getBaseContext(), "local service_disconected",//Binding.this
Toast.LENGTH_SHORT).show();
}
};
セカンダリ アクティビティ:
public class SecondaryActivity extends Activity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
serverIntent = new Intent(this, MainActivity.class);
serverIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(serverIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
私の質問は、mIsBound 値が常に復元されるようにするにはどうすればよいですか? SharedPreferences の使用はオプションであることがわかりましたが、これは正しい方法ではないようです。
サービスが既に実行されている場合、サービスに正しくバインドする方法に関する他の提案も大歓迎です!
ありがとう!
アップデート:
問題を解決する別の方法を考えました。それは、サービスが既に実行されているかどうかを確認し、true の場合は (再) バインドすることです。これを行う方法は次のとおりです: Android でサービスが実行されているかどうかを確認するには?
これがこれを行うための正しいまたは最良の方法であるとはまだ確信していませんが、今のところはそうします。これについてコメントをいただければ幸いです。