请你向我解释一下,当我们绑定服务时会发生什么,但是从不启动它然后解除绑定?我得到“活动已泄露服务连接错误”,但我不明白为什么。
为MyService: package 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;
}
}
MainActivity:
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;
}
}
BoundActivity:
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;
}
}
当我点击button1时,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)
答案 0 :(得分:2)
问题就在这里。
if (mBound){
unbindService(mConnection);
}
服务受bindService绑定,但onServiceConnected
从不调用,ao
mBound = true:
永远不会发生。这就是为什么unbindService
没有被调用,导致泄密的原因。