gpt4 book ai didi

android - 服务连接泄漏

转载 作者:行者123 更新时间:2023-11-29 00:23:06 32 4
gpt4 key购买 nike

我有一个非常基本的服务程序,但我收到这条警告消息

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 行看到一些问题。但我不知道如何解决。请帮忙。

最佳答案

您还没有解绑您的服务。服务连接泄漏仅在您忘记停止或取消绑定(bind)服务时发生。

查看您的代码..当您尝试取消绑定(bind)服务时,您的 mBounded 为假,因为您在 serviceconnection 接口(interface)的 servicedisconnected 方法中将其设置为假,这会阻碍您取消绑定(bind)服务。

在这里看看你做了什么:

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;

您必须了解 connection disconnected()Service unbind() 不同。

绑定(bind)服务时必须使其为真。即:

Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection,Context.BIND_AUTO_CREATE);
mBound = true;

并且在取消绑定(bind)服务后使 bool 变量 false 不在 serviceconnection.like 中:

 if(mBound){
unbindService(mConnection);
mBounded = false;
}

它会解决你的问题..

关于android - 服务连接泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21698787/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com