Android学习笔记二十六.跨进程调用Service(AIDL Service)
package com.example.android_aidlservice; import java.util.Timer; import java.util.TimerTask; import com.example.android_aidlservice.ICat.Stub; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class AidlService extends Service { //1.声明一个IBinder对象、Timer对象 private CatBinder catBinder; private Timer timer; //2.定义两个数组与两个私有变量 private String color; private double weight; String[] colors = new String[]{"黑色","黄色","红色"}; double[] weigths=new double[]{2.3,3.1,1.58}; //3.继承stub,也就实现了ICat接口,并实现了IBinder接口 public class CatBinder extends Stub { @Override public String getColor() throws RemoteException { return color; } @Override public double getWeight() throws RemoteException { return weight; } } //4.当Service创建时调用该方法 @Override public void onCreate() { super.onCreate(); //a.实例化一个CatBinder对象 catBinder = new CatBinder(); //b.该service完成的任务:随机地改变Service组件内color、weight属性的值 timer.schedule(new TimerTask(){ @Override public void run() { int rand=(int)(Math.random()*3); color=colors[rand]; weight = weigths[rand]; System.out.println("-----------"+rand); } }, 0, 800); } /*5.访问者使用bindService()方法启动Service时,该方法用于返回catBinder对象 * (1)在绑定本地Service的情况下,该catBinder对象会直接传给客户端的ServiceConnection对象; * (2)在绑定远程Service的情况下,只将catBinder对象的代理传递给客户端的ServiceConnection对象的 * onServiceConnection方法的第二个参数*/ @Override public IBinder onBind(Intent intent) { return catBinder; } //6.回调该方法关闭Service @Override public void onDestroy() { timer.cancel(); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_aidlservice" 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" > <service android:name=".AidlService" android:exported="true"> <intent-filter> <action android:name="com.example.service.AIDL_SERVICE"/> </intent-filter> </service> </application> </manifest>
package com.example.android_aidl_service; 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.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class AidlClient extends Activity { private ICat catService; //声明一个ICat对象 private Button get; private EditText color,weight; //1.创建一个ServiceConnection对象 private ServiceConnection conn=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { //获取远程Service的onBind方法的对象的代理 catService = ICat.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { catService=null; } }; //2.绑定远程Service并获取其内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //a.获取Activity主界面组件 setContentView(R.layout.main); get=(Button)findViewById(R.id.get); color=(EditText)findViewById(R.id.color); weight=(EditText)findViewById(R.id.weight); //b.创建所需绑定的Service的Intent并设置其Action属性(即指定启动哪个远程Service) Intent intent=new Intent(); intent.setAction("com.example.service.AIDL_SERVICE"); //c.绑定并启动远程Service bindService(intent,conn,Service.BIND_AUTO_CREATE); //d.通过IBinder对象的代理获取远程Service数据 get.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { try { color.setText(catService.getColor()); weight.setText(catService.getWeight()+""); } catch (RemoteException e) { e.printStackTrace(); } } }); } //3.退出Activity时调用该方法,解除远程Service的绑定 @Override protected void onDestroy() { super.onDestroy(); this.unbindService(conn); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_aidl_service" 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=".AidlClient" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。