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
- Android—Service的使用(二)
- Android Service 的使用(二)----Service的绑定使用
- android service的使用(二)
- Android Service(二) Service使用
- Android Service解析(二):远程Service的使用
- Android——Service 使用(二)
- Service的使用二
- Android—Service的使用(一)
- Android Service(二)
- android service (二)
- Android Service(二)
- android service(二)
- android Service(二)
- android的一些总结——service(二)之activity与service之间的通信
- Android 绑定Service的实现方法二:使用Messenger
- android的Service学习二
- Android Service的使用
- Android Service 的使用
- C语言 Floyd最短路
- 安卓开发环境初始化问题
- Android 闹钟 开发过程记录(二)
- iOS ARC内存管理循环引用的问题
- Android之TabHost布局
- Android—Service的使用(二)
- Python--ZOJ1051
- UVa1593 - Alignment of Code
- 健康问题
- PHP基础语法
- SVN无法提交 Can't set file "/db/txn_current" read-write :拒绝访问
- iOS ARC内存管理基本原理
- java编程思想-内部类-学习摘要
- Object - C 的消息处理机制与 Method Swizzling(函数混淆)