我对Android开发和StackOverflow都很陌生,我希望我不会问一个之前被问过的愚蠢问题,但我找不到任何内容。
我正在创建一个应用程序,当连接到蓝牙设备时启动(最初未绑定)服务,并且只有当我告诉它断开连接或它失去连接时才应停止。 服务启动后,主活动绑定到它,并在调用onDestroy()时解除绑定。当调用onStart并且mIsBound为true时,它也会重新绑定。 mIsBound布尔值与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);
}
mainActivity:
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();
}
};
secondaryActivity:
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。 这里解释了这样做的方式: How to check if a service is running on Android?
我仍然没有放弃确信这是正确或最好的方法,但它现在会做。 如果你对此有任何评论,我们表示赞赏。
答案 0 :(得分:1)
我对你帖子的主题有同样的问题(当使用onreest按钮onRestoreInstanceState时没有被称为),尽管原因与你在身体中描述的原因不同。由于我没有找到任何其他帖子回答它,我将在此处发布我的解决方案。
问:使用up按钮onRestoreInstanceState未被调用
A:按操作栏上的主页(向上导航)按钮确实与按后退按钮的行为不同。前者不会调用父活动的onRestoreInstanceState()
,而按后退按钮也可以。
如果要完成与后退按钮相同的行为,可以通过覆盖子活动中的onNavigateUp()
方法并从中调用onBackPressed()
来实现。
在儿童活动中:
@Override
public boolean onNavigateUp() {
onBackPressed();
return true;
}
我不确定返回值,true
似乎工作正常并且满足文档:“如果向上导航成功完成并且此活动已完成,则为true,否则为”