android ondestroy service,onDestroy在android中运行时,MainActivity泄露了ServiceConnection。如何防止这个错误?...

我正在制作一个android-wear应用程序,主要功能在

ConnectionService

类,它基本上处理与websocket的通信。我的问题是,如果我用android-wear设备上的按钮退出应用程序,没有错误,所有的操作都会在后台按预期运行。但是,如果我左击,退出应用程序到菜单,则应用程序崩溃(据我所知,在本例中

onDestroy

方法正在运行。所以我得到了以下错误:

Activity com.example.adambodnar.canary.MainActivity has leaked ServiceConnection com.example.adambodnar.canary.MainActivity$4@79552f9 that was originally bound here

android.app.ServiceConnectionLeaked: Activity com.example.adambodnar.canary.MainActivity has leaked ServiceConnection com.example.adambodnar.canary.MainActivity$4@79552f9 that was originally bound here

我读了很多关于这个的问题,但是我找不到一个与我的问题相匹配的解决方案。服务作为前台服务在后台运行,因此我不想阻止它运行。

这是

MainActivity.class

public class MainActivity extends FragmentActivity {

private static ConnectionService mService;

private boolean mBound = false;

private final FragmentManager supportFragmentManager = getSupportFragmentManager();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

supportFragmentManager.beginTransaction().replace(R.id.fragment_container, new MainFragment()).commit();

if(!PermissionHelper.hasPermissions(this, Constants.PERMISSIONS)){

ActivityCompat.requestPermissions(this, Constants.PERMISSIONS, Constants.PERMISSION_ALL);

}

Intent intent = new Intent(this, ConnectionService.class);

this.startService(intent);

this.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

}

private ServiceConnection mConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName className,

IBinder service) {

ConnectionService.LocalBinder binder = (ConnectionService.LocalBinder) service;

mService = binder.getServiceInstance();

mBound = true;

}

@Override

public void onServiceDisconnected(ComponentName arg0) {

mBound = false;

}

};

public static void sendMessage(String message) {

mService.sendMessage(message);

}

public static void rebuildWebSocketConnection() {

mService.rebuildWebSocketConnection();

}

}

还有我的

连接服务

:

public class ConnectionService extends Service {

private WebSocketHelper webSocketHelper = new WebSocketHelper();

private final String TAG = "ConnectionService";

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {

public ConnectionService getServiceInstance() {

return ConnectionService.this;

}

}

@Nullable

@Override

public IBinder onBind(Intent intent) {

Log.i(TAG, "SERVICE IS BINDED");

return mBinder;

}

@Override

public void onCreate() {

System.out.println("ON CREATE OF CONNECTION SERVICE");

super.onCreate();

Intent notificationIntent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

notificationIntent, 0);

Notification notification = new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("My Awesome App")

.setContentText("Doing some work...")

.setContentIntent(pendingIntent).build();

startForeground(1337, notification);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.i(TAG, "--- Connection Service onStartCommand ---");

super.onStartCommand(intent, flags, startId);

return START_STICKY;

}

public void sendMessage(String message) {

Log.d(TAG, "WebSocketHelper is open: " + webSocketHelper.isWebSocketOpen());

if (webSocketHelper.isWebSocketOpen()) {

Log.i(TAG, "--- WebSocket Message: " + message + " ---");

webSocketHelper.sendMessage(message);

}

}

}

那么我该怎么做才能让这个错误消失呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值