您能否向我解释一下,当我们绑定到服务但从不启动它然后解除绑定时会发生什么?我收到“活动泄露了服务连接错误”,但我不明白为什么。
MyService:包com.example.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service{
Binder mLocalBinder = new Binder();
@Override
public IBinder onBind(Intent intent) {
return mLocalBinder;
}
}
主要活动:
package com.example.servicetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, BoundActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
绑定活动:
package com.example.servicetest;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
public class BoundActivity extends Activity {
boolean mBound = false;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bound);
}
@Override
protected void onResume(){
super.onResume();
Intent service = new Intent(this, MyService.class);
bindService(service, mConnection, 0);
}
@Override
protected void onPause(){
if (mBound){
unbindService(mConnection);
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bound, menu);
return true;
}
}
当我单击按钮 1 时,BoundActivity 启动并绑定到 MyService。当我单击返回时,出现以下异常:
10-19 21:50:17.445: E/ActivityThread(24138): Activity com.example.servicetest.BoundActivity has leaked ServiceConnection com.example.servicetest.BoundActivity$1@41deb340 that was originally bound here
10-19 21:50:17.445: E/ActivityThread(24138): android.app.ServiceConnectionLeaked: Activity com.example.servicetest.BoundActivity has leaked ServiceConnection com.example.servicetest.BoundActivity$1@41deb340 that was originally bound here
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ContextImpl.bindService(ContextImpl.java:1440)
10-19 21:50:17.445: E/ActivityThread(24138): at android.content.ContextWrapper.bindService(ContextWrapper.java:496)
10-19 21:50:17.445: E/ActivityThread(24138): at com.example.servicetest.BoundActivity.onResume(BoundActivity.java:37)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.Activity.performResume(Activity.java:5211)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-19 21:50:17.445: E/ActivityThread(24138): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 21:50:17.445: E/ActivityThread(24138): at android.os.Looper.loop(Looper.java:137)
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-19 21:50:17.445: E/ActivityThread(24138): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 21:50:17.445: E/ActivityThread(24138): at java.lang.reflect.Method.invoke(Method.java:525)
10-19 21:50:17.445: E/ActivityThread(24138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-19 21:50:17.445: E/ActivityThread(24138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 21:50:17.445: E/ActivityThread(24138): at dalvik.system.NativeStart.main(Native Method)