我正在制作一个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);
}
}
}
那么我该怎么做才能让这个错误消失呢?