出现 leaked ServiceConnection 解决办法
来源:互联网 发布:js让隐藏的div显示 编辑:程序博客网 时间:2024/06/03 11:16
在Android界面上调用service绑定方法,退出界面时候报告一堆错误 Activity has leaked ServiceConnection 等,应该重写onDestroy方法,取消绑定,这样就ok了。
private Button btn1;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.button1); btn2 = (Button)findViewById(R.id.button2); btn3 = (Button)findViewById(R.id.button3); btn4 = (Button)findViewById(R.id.button4); btn1.setOnClickListener(btnClickListener); btn2.setOnClickListener(btnClickListener); btn3.setOnClickListener(btnClickListener); btn4.setOnClickListener(btnClickListener); }
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onDestroy(){ super.onDestroy(); unbindService(serviceconnection); }
package com.jiuqi.testservice01;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn2;
private Button btn3;
private Button btn4;
private MyService myservice ;
public ServiceConnection serviceconnection = new ServiceConnection(){
@Override
public void onServiceDisconnected(ComponentName name){
myservice = null;
Toast.makeText(MainActivity.this, "MyService failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service){
myservice = ((MyService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "MyService connected", Toast.LENGTH_SHORT).show();
}
};
private OnClickListener btnClickListener = new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(MainActivity.this,MyService.class);
switch(v.getId()){
case R.id.button1:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
case R.id.button3:
bindService(intent, serviceconnection, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(serviceconnection);
break;
default:
break;
}
}
};
@Override
}
0 0
- 出现 leaked ServiceConnection 解决办法
- 出现leaked ServiceConnection 的解决办法
- 出现leaked ServiceConnection 的解决办法
- 出现leaked ServiceConnection 的解决办法
- leaked ServiceConnection
- has leaked ServiceConnection
- 百度地图api出现 leaked ServiceConnection com.baidu.location.LocationClient错误
- 【Android】- bindService 之 leaked ServiceConnection错误
- 在使用百度地图的SDK时,出现.LocationDemo has leaked ServiceConnection 异常并且无法定位的时候
- 解决百度地图使用出现的has leaked ServiceConnection com.baidu.location.b@4aa5a810 that was originally bound here
- 使用对话框时出现window leaked的解决办法
- Service com.android.exchange.ExchangeService has leaked ServiceConnection
- Service com.android.exchange.ExchangeService has leaked ServiceConnection
- 解决has leaked ServiceConnection com.baidu.location.c@427b6780
- MainActivity has leaked ServiceConnection com.baidu.location.LocationClient
- 解决has leaked ServiceConnection com.baidu.location.c@427b6780
- Service com.android.exchange.ExchangeService has leaked ServiceConnection
- Service com.android.exchange.ExchangeService has leaked ServiceConnection
- eclipse+gradle+gretty的web项目运行命令
- NYOJ 722 数独
- Android中shape的基本使用
- iOS开发UI基础—32直接使用UITableView Controller
- unix中统计行数,字符和字数
- 出现 leaked ServiceConnection 解决办法
- UIApplication深入研究
- NYOJ 45 棋盘覆盖
- DDXML导入报Undefined symbols for architecture arm64等错误的的解决办法
- 解析nginx负载均衡
- iOS开发UI高级—01常见的项目文件介绍
- adb install tab
- NYOJ 46 最少乘法次数
- net操作xml文件