Android—Service的使用(二)

来源:互联网 发布:淘宝网汽车贴膜 编辑:程序博客网 时间:2024/05/17 02:48

继续上一篇(Android—Service的使用(一))之后,这一篇讲解Service的绑定本地service并与之通讯

首先看看ServiceTest有哪些改动

package com.example.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class ServiceTest extends Service {private int count;private boolean quit=false;   //判断是否退出private MyBinder binder = new MyBinder();public class MyBinder extends Binder{   //继承Binder实现IBinder类public int getCount(){return count; //获取运行状态}}public ServiceTest() {// TODO Auto-generated constructor stub}
//绑定时回调@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("Service is binded");return binder;}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);System.out.println("service is start!");}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("service is create!");new Thread(){@Overridepublic void run() {// TODO Auto-generated method stubwhile(!quit){try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}count++;}}}.start();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("service is destroy!");this.quit = true;}<span style="white-space:pre"></span>//解除绑定@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("service is onUnbind!");return super.onUnbind(intent);}}
再看看mainactivity里面代码:

package com.example.service;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends ActionBarActivity implements OnClickListener {Intent intent = new Intent();private Button button1;private Button button2;private Button button3;private TextView tv;ServiceTest.MyBinder  binder;private ServiceConnection  con = new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubSystem.out.println("service connection!");//链接成功回调binder = (ServiceTest.MyBinder)service;  //获取service的mybinder方法返回的mybind对象}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubSystem.out.println("service disconnection");//断开回调}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView)findViewById(R.id.tv);button1 = (Button)findViewById(R.id.button1);button2 = (Button)findViewById(R.id.button2);button3 = (Button)findViewById(R.id.button3);button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);intent.setAction("com.example.service.ServiceTest");System.out.println("fffffffffffffffffffffffffffffffffffff");//startService(intent);//启动service}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();//stopService(intent);//停止serviceSystem.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1:bindService(intent, con, Service.BIND_AUTO_CREATE);  //绑定break;case R.id.button2:unbindService(con);//解除绑定break;case R.id.button3:tv.setText("count:"+binder.getCount());System.out.println("count:"+binder.getCount());  //获取数据break;default:break;}}}


结果:


源码地址:

http://download.csdn.net/detail/weiguishan/8464287

0 0