Android笔记三十.Service入门(四).跨进程调用Service(AIDL Service)
<span style="font-family:Times New Roman;font-size:18px;">package com.example.aidlservice; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import com.example.aidlservice.ICat.Stub; public class AidlService extends Service{ private Timer timer=new Timer();; //2.定义两个数组与两个私有变量 private String color; private double weight; String[] colors = new String[]{"黑色","黄色","红色"}; double[] weigths=new double[]{2.3, 3.1,1.58}; //1.声明一个IBinder对象、Timer对象 private CatBinder binder= new CatBinder(); //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() { System.out.println("调用Service的onCreate方法"); super.onCreate(); //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) { System.out.println("返回Service的IBinder对象"); return binder; } //6.回调该方法关闭Service @Override public void onDestroy() { timer.cancel(); } }</span>
<span style="font-family:Times New Roman;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aidlservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ServiceTest" 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=".AidlService"> <intent-filter> <action android:name="com.jiangdongguo.service"/> </intent-filter> </service> </application> </manifest></span>
<span style="font-family:Times New Roman;font-size:18px;">package com.example.aidlclient; 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.TextView; import com.example.aidlservice.ICat; public class AidlClient extends Activity { private ICat catService; //声明一个ICat对象 private Button get; private TextView 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) { } }; //2.绑定远程Service并获取其内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //a.获取Activity主界面组件 setContentView(R.layout.main); get=(Button)findViewById(R.id.get); color=( TextView)findViewById(R.id.color); weight=(TextView)findViewById(R.id.weight); //b.创建所需绑定的Service的Intent并设置其Action属性(即指定启动哪个远程Service) Intent intent=new Intent("com.jiangdongguo.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); } }</span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。