出现 leaked ServiceConnection 解决办法
(2013-05-08 14:49:49)
标签:
androidservicebindunbindleakedserviceconnect |
分类: 工作 |
see also
: http://book.51cto.com/art/201006/206887.htm
在Android界面上调用service绑定方法,退出界面时候报告一堆错误 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
}
前一篇:正则表达式的JS验证
后一篇:对百度手机输入法的一点改进建议