我有一个非常基本的服务程序,但我收到此警告消息
02-11 04:58:23.383: E/ActivityThread(1686): Activity com.example.serviceexample.Client has leaked ServiceConnection com.example.serviceexample.Client$1@416e56e8 that was originally bound here
02-11 04:58:23.383: E/ActivityThread(1686): android.app.ServiceConnectionLeaked: Activity com.example.serviceexample.Client has leaked ServiceConnection com.example.serviceexample.Client$1@416e56e8 that was originally bound here
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ContextImpl.bindService(ContextImpl.java:1440)
02-11 04:58:23.383: E/ActivityThread(1686): at android.content.ContextWrapper.bindService(ContextWrapper.java:496)
02-11 04:58:23.383: E/ActivityThread(1686): at com.example.serviceexample.Client.onStart(Client.java:72)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.Activity.performStart(Activity.java:5143)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-11 04:58:23.383: E/ActivityThread(1686): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 04:58:23.383: E/ActivityThread(1686): at android.os.Looper.loop(Looper.java:137)
02-11 04:58:23.383: E/ActivityThread(1686): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-11 04:58:23.383: E/ActivityThread(1686): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 04:58:23.383: E/ActivityThread(1686): at java.lang.reflect.Method.invoke(Method.java:525)
02-11 04:58:23.383: E/ActivityThread(1686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-11 04:58:23.383: E/ActivityThread(1686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-11 04:58:23.383: E/ActivityThread(1686): at dalvik.system.NativeStart.main(Native Method)
代码部分:
public class Client extends Activity {
boolean mBounded;
Server mServer;
TextView text;
Button button;
LocalBinder mLocalBinder;
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(Client.this, "Service is disconnected", 1000).show();
mBounded = false;
mServer = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(Client.this, "Service is connected", 1000).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "onCreate Called", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
mServer = new Server();
// mServer = mLocalBinder.getServerInstance();
try{
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
text.setText(mServer.getTime());
}
catch(Exception e)
{e.printStackTrace();}
}
});
}
catch(Exception e)
{e.printStackTrace();}
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart Called", Toast.LENGTH_SHORT).show();
Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection,BIND_ADJUST_WITH_ACTIVITY);// BIND_AUTO_CREATE);// This is Line 72
};
@Override
protected void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
Toast.makeText(this, "onStop Called", Toast.LENGTH_SHORT).show();
}
}
protected void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "onStop Called", Toast.LENGTH_SHORT).show();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
Toast.makeText(this, "onStop Called", Toast.LENGTH_SHORT).show();
}
}
}
服务器类:
public class Server extends Service{
IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public Server getServerInstance() {
return Server.this;
}
}
public String getTime() {
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return mDateFormat.format(new Date());
}
}
请让我知道如何解决此logcat消息。我可以在第72行看到一些问题。但我不知道如何解决它。请帮忙。
答案 0 :(得分:2)
您还没有取消绑定服务。只有在您忘记停止或取消绑定服务时才会发生服务连接泄漏。
请参阅您的代码..当您尝试解除绑定服务时,您的mBounded
为false,因为您在serviceconnection接口的servicedisconnected方法中将其设为false,这会阻碍您取消绑定服务。
在这看到你做了什么:
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(Client.this, "Service is disconnected", 1000).show();
mBounded = false;//here you make false to the boolean variable which make obstacle to unbind your service
mServer = null; }
从此处删除 mBounded = false;
您必须了解连接已断开连接()与服务取消绑定()不同。
绑定服务时必须使其成为现实。即:
Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection,Context.BIND_AUTO_CREATE);
mBound = true;
在unbind服务之后使boolean变量为false而不是在serviceconnection.like:
中 if(mBound){
unbindService(mConnection);
mBounded = false;
}
它会解决你的问题..