Android学习笔记二十四.Service入门(二)绑定本地Service并与之通信
package com.example.android_service_bind; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class BindService extends Service { private int count; private boolean quit; private MyIBinder binder=new MyIBinder(); //声明一个IBinder对象 //1.定义一个IBinder子类并实现一个获取Service运行状态(count)的方法 public class MyIBinder extends Binder { public int getCount() { return count; //返回Service运行状态:count } } //2.Service子类必须实现的一个类,用于返回IBinder对象 public IBinder onBind(Intent intent) { System.out.println("Service is Binded!"); return binder; //返回IBinder对象 } //3.Service被创建时回调该方法 @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created."); //创建并启动一个线程,实现动态修改count状态值 new Thread() { @Override public void run() { while(!quit) //标识Service关闭启动状态 { try { Thread.sleep(1000); }catch(InterruptedException e) { } count++; } } }.start(); } //4.Service被断开连接时回调方法 @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbinded"); return true; } //5.Service被关闭之前回调该方法 @Override public void onDestroy() { super.onDestroy(); this.quit=true; //当调用该方法后(!quit)为假,线程结束 System.out.println("Service is Destroy"); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_service_bind" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".BindServiceTest" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 配置一个Service组件 --> <service android:name=".BindService"> <intent-filter> <!-- 为该Service组件的intent-filter配置action --> <action android:name="com.example.service.BIND_SERVICE"/> </intent-filter> </service> </application> </manifest>
package com.example.android_service_bind; import android.app.Activity; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BindServiceTest extends Activity { Button bind,unbind,status; //1.保持所启动的Service的IBinder对象 BindService.MyIBinder binder; //2.定义一个ServiceConnection对象 private ServiceConnection conn = new ServiceConnection() { //a.当该Activity与Service连接成功时回调该方法 @Override public void onServiceConnected(ComponentName name , IBinder service) { System.out.println("---Service is connected---"); //获取Service的onBind方法所返回的MyBinder对象 binder=(BindService.MyIBinder)service; } //b.当该Activity与Service连接不成功时回调该方法 @Override public void onServiceDisconnected(ComponentName name) { System.out.println("---Service is Disconnected---"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //a.获取程序界面中的start、stop、getServiceStatus按钮 bind = (Button)findViewById(R.id.bind); unbind = (Button)findViewById(R.id.unbind); status = (Button)findViewById(R.id.getServiceStatus); //b.创建启动Service的Intent final Intent intent = new Intent(); intent.setAction("com.example.service.BIND_SERVICE"); //c.绑定指定Service bind.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { bindService(intent,conn,Service.BIND_AUTO_CREATE); } }); //d.解除绑定的Service unbind.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { unbindService(conn); } }); //e.获取Service的状态,显示Service的count值 status.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(BindServiceTest.this,"Service的count值为:"+binder.getCount() , Toast.LENGTH_SHORT).show(); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="绑定service"/> <Button android:id="@+id/unbind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="解除绑定"/> <Button android:id="@+id/getServiceStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="获取状态"/> </LinearLayout> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。