结论现行:关闭activity的时候,需要解绑服务
1.应该重写onDestroy方法,取消绑定,这样就ok了。
2.可以通过广播机制。
Android】关于Activity Crash后,其调用绑定的Service依然在后台运行的问题???? 我的Activity里同时使用了bindService和startService,调用了startService 之后就只能代码里调用stopService才能将其停掉,这样的后果就是一旦Activity崩溃(出错被系统kill等),Service还在后台运行,即便Crash了也会自动重启,曾尝试在Activity的OnDestroy方法里面调stopService,但是由于项目里Activity的退出逻辑比较复杂,不能在Activity的OnDestroy里这样做。于是,我的问题就来了:如何在Service里监听我绑定调用这个Service的Activity是被系统干掉还是我自己主动干掉的??有这样的监听方法吗???如果有的话,那么我就可以在Service里面通过该监听类的方法主动停掉自己,这样就不会导致我Activity被kill了后Service还在运行进而导致再次进入app的时候造成逻辑混乱。 问题可以总结为: 如何检测Activity是Crashed还是自己主动退出的?? Crash Detector谁做过。 采用广播的方法 在Service中动态注册广播registerReceiver(receiver, filter); receiver = new InnerReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction(GlobalConsts.ACTION_PLAY); registerReceiver(receiver, filter); Service中自定义一个动态的内部的广播接收器InnerReceiver继承BroadcastReceiver 重写 onReceive方法,处理广播 String action=intent.getAction(); if(GlobalConsts.ACTION_STOP.equals(action)){ //销毁 stopSelf();}
Activity中OnDestroy发送广播 Intent intent =new Intent(GlobalConsts.ACTION_STOP); sendBroadcast(intent); http://www.cnblogs.com/draem0507/archive/2013/05/25/3099461.html 这里有详细一点的说明 追问 OnDestroy是任何情况下Crash确定一定以及肯定会掉调用的吗? 比如因为系统内存不足被kill掉,或者死在加载的jni库中时都会调用OnDestroy吗? 就是Java层和c层都要保证的方法才行。 本回答由提问者推荐 抢首赞 评论 分享 举报 匿名用户 2014-01-10 也许你可以试试LocalService。 Local Service: Service is running in the same process as other components (i.e. activity that bound to it) from the same application, when this single application-scoped process has crashed or been killed, it is very likely that all components in this process (include the activity that bound to this service) are also destroyed. 收起追问追答� 追问 good suggestion, 但是我还是想知道怎样检测Activity的Crash. 追答 用UncaughtExceptionHandler来检测就好。 追问 不知道UncaughtExceptionHandler这个是否能检测任何情况下的crash? 它能检测JNI调用的C层出现的异常吗,还有由于内存泄露导致的内存不足而使程序挂掉的情况这个也能检测到吗?
问题出自
1.http://blog.sina.com.cn/s/blog_439f80c40101jkpr.html
2:https://www.runoob.com/w3cnote/android-tutorial-service-1.html
TestService2.java:
public class TestService2 extends Service {
private final String TAG = "TestService2";
private int count;
private boolean quit;
//定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder
{
public int getCount()
{
return count;
}
}
//必须实现的方法,绑定改Service时回调该方法
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind方法被调用!");
return binder;
}
//Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate方法被调用!");
//创建一个线程动态地修改count的值
new Thread()
{
public void run()
{
while(!quit)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
count++;
}
};
}.start();
}
//Service断开连接时回调
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind方法被调用!");
return true;
}
//Service被关闭前回调
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true