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);
}
}
}
那么我该怎么做才能让这个错误消失呢?
android ondestroy service,onDestroy在android中运行时,MainActivity泄露了ServiceConnection。如何防止这个错误?...相关推荐
- 在react-nactive项目中运行时,报错error Failed to install the app
在react-nactive项目中运行时,报错 error Failed to install the app. Make sure you have the Android development ...
- android 解决APP退出后以及后台运行时,再次点击图标的运行问题
需求:现有闪屏界面SplashActivity,要求在无后台运行APP的情况下,点开应用,首先出现闪屏,2秒过后,进入到主功能界面MianActivity,运行时,将APP关至后台,再次点击图标,返回 ...
- android cpu hotplug,[MTK] [CPU DVFS/Hotplug]运行时,把CPU固定在特定频率/特定核数的办法...
[MTK] [CPU DVFS/Hotplug]运行时,把CPU固定在特定频率/特定核数的办法 2020-06-19 阅读:2580 [通过以下sysfs操作就可以把CPU固定在特定的频率上] ech ...
- android 启动service报错,Android小经验
转载自:http://mp.weixin.qq.com/s?__biz=MzA4MjU5NTY0NA==&mid=404388098&idx=1&sn=8bbbba7692dc ...
- android studio service directory path,Android Studio User目录缓存搬移到指定目录
User目录下主要存在三个文件 1..android是Android SDK配置模拟器生成的,一般不用模拟器,子目录avd为空,不需要处理. 2..AndroidStudio需要去AS安装目录下bin ...
- python用cmd运行失败_python-在CMD中运行时,脚本失败并出现Attribut...
人们问起python版本是因为tk.filedialog在2.x中拼写不同.但是,我怀疑您的问题是空闲在托管环境中运行代码,该环境掩盖了未正确发布tkinter.filedialog的未发布代码中的错 ...
- python游戏结束显示分数代码_当游戏循环在Python中运行时,多线程来显示游戏分数?...
我想根据比赛时间的推移得分.为此,我想让两个循环同时运行.游戏循环和得分循环,每1.5秒加1.当我运行程序时,分数不会出现.我是否正确使用多线程?这是最好的办法吗?为了简单起见,我只发布了相关代码.谢 ...
- android电脑文件列表不刷新,Android 利用RecyclerView.Adapter刷新列表中的单个view问题...
首先使用RecyclerView的adapter继承:RecyclerView.Adapter public class OrderListAdapter extends RecyclerView.A ...
- ASP.NET HTTP 运行时
ASP.NET HTTP 运行时 一个请求从 URL 字符串到 HTML 代码的"漫长曲折"之路 Dino Esposito Wintellect 2003年7月10日 摘要:本文 ...
最新文章
- 彻底搞懂JVM类加载器:基本概念
- python字典排序取最值总结
- Recyclerview不显示内容
- 堆内存破坏检测实战--附完整调试过程
- linux系统基础优化小结
- riak php7,Laravel中服务提供者的register和boot分别是干什么
- 转:Vim中显示不可见字符
- underscore.js 源码分析5 基础函数和each函数的使用
- 找出一棵树某一深度所有节点
- Android使用SurfaceView实现墨迹天气的风车效果
- 在VMware Workstation 9中安装Mac OS X 10.8 Mountain Lion
- 功能升级 | Choerodon猪齿鱼“新”知识管理介绍
- python模拟用户登录代码_Python模拟用户自动登陆网易126邮箱源码详解
- 江苏科技大学MATLAB考试,江苏科技大学精品课程申报表.DOC
- 解决Veil—Evasion安装中git clone导致失败的问题
- 基于 Next.js实现在线Excel
- 【TiDB@丰巢】支付平台的迁移之旅
- Clickhouse其它类型表引擎(Live View、Null、URL)
- php获取农历日期节日
- 医学影像组学人工智能案例结合