与未绑定的服务访问获取状态。
写了两个活动,一个活动开启未绑定服务,第二个活动绑定服务,获取服务的状态。
自定义的服务:
public class MyService extends Service {
private Thread t;
private boolean isRun = false;
private MyBinder binder = new MyBinder();
private int Num = 0;
// 必须实现的方法,返回一个IBinder对象,应用程序通过该对象与service组件通信。
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("onBind", "******");
return binder;
}
// 第一次被创建的时候,回调该方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("onCreate", "******");
}
// startService()启动的时候,回调该方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
myThread thread = new myThread();
t = new Thread(thread);
t.start();
Log.i("onStartCommand", "******");
return super.onStartCommand(intent, flags, startId);
}
// 当绑定的所有客户端都断开连接的时候,回调该方法
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("onUnbind", "******");
return super.onUnbind(intent);
}
// 关闭之前,回调该方法
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("onDestroy", "******");
isRun = true;
super.onDestroy();
}
// 新建线程类,在里面进行耗时操作
class myThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (!isRun) {
Num++;
Log.i("(^~^)", "show run!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public int getNum() {
return Num;
}
// 自定我的binder
class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
}
第一个活动:
public class TestService extends Activity implements OnClickListener {
private Button start;
private Button stop;
private Button skip;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_testservice);
initView();
}
public void initView() {
start = (Button) findViewById(R.id.btn_start);
stop = (Button) findViewById(R.id.btn_stop);
skip = (Button) findViewById(R.id.btn_skip);
start.setOnClickListener(this);
stop.setOnClickListener(this);
skip.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent();
intent.setAction("service.testmyservice");
if (v == start) {
startService(intent);
} else if (v == stop) {
stopService(intent);
} else if (v == skip) {
intent = new Intent(this, TestSecondService.class);
startActivity(intent);
}
}
}
第二个活动:
/**
* 绑定未绑定服务
*/
public class TestSecondService extends Activity implements OnClickListener {
private Button start;
private Button stop;
private Button receive;
private Intent intent;
private MyService myService;
private MyBinder myBinder;
private ServiceConnection con = new ServiceConnection() {
// 连接成功的时候,回调该方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
// 获取binder()对象
myBinder = (MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_testservice);
initView();
}
public void initView() {
start = (Button) findViewById(R.id.btn_start);
stop = (Button) findViewById(R.id.btn_stop);
receive = (Button) findViewById(R.id.btn_skip);
receive.setText("接受服务状态");
start.setOnClickListener(this);
stop.setOnClickListener(this);
receive.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent();
intent.setAction("service.testmyservice");
if (v == start) {
// 绑定指定的服务
bindService(intent, con, Service.BIND_AUTO_CREATE);
} else if (v == stop) {
// 解除绑定
unbindService(con);
} else if (v == receive) {
// 获取内容
myService = myBinder.getService();
// 获取服务的状态
Log.i("my num:", myService.getNum() + "****");
}
}
}
注意:
1.生命周期:
oncreate()--->onstart()--->onbind()--->onunbind()--->ondestroy()。
2.推荐博客: