700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android 之路56---AIDL建立远程通信

Android 之路56---AIDL建立远程通信

时间:2021-02-17 02:35:12

相关推荐

Android 之路56---AIDL建立远程通信

导读

1.案例

案例

app AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="com.hala.srevicedemo"><application android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".MyService"android:enabled="true"android:exported="true"><intent-filter><action android:name="com.hala.myservice"/></intent-filter></service></application></manifest>

app 布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.hala.srevicedemo.MainActivity"><Button android:id="@+id/start"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="启动服务"android:onClick="onClick"/><Button android:id="@+id/stop"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="停止服务"android:onClick="onClick"/><Button android:id="@+id/bind"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="绑定服务"android:onClick="onClick"/><Button android:id="@+id/unbind"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="解锁服务"android:onClick="onClick"/></LinearLayout>

app MainActivity.java

package com.hala.srevicedemo;import ponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity {private ServiceConnection conn=new ServiceConnection() {//当客户端正常连接着Service时,执行服务的绑定操作会被调用//如果没有ServiceConnection,会出现绑定后解绑,无法再次绑定的情况@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d("TAG","hello,bind again");//以下是关于aidl操作IMyAidlInterface imal=IMyAidlInterface.Stub.asInterface(service);try {imal.showProgress();} catch (RemoteException e) {e.printStackTrace();}/*以下是关于Binder操作的代码只能在同进程间传递(MyService中也有修改)//强制转换MyService.MyBinder mb=(MyService.MyBinder)service;int step=mb.getProcess();Log.d("TAG","当前进度为:"+step);*/}//当客户端和服务的连接丢失了@Overridepublic void onServiceDisconnected(ComponentName name) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onClick(View view){//start 与 stop为一组//bind 与 unbind为一组switch (view.getId()){case R.id.start://如果服务已经创建,重复启动不会重新创建,操作的是同一个服务,除非先销毁Intent it=new Intent(this,MyService.class);startService(it);break;case R.id.stop://这里虽然是两个intent,但确实针对同一个对象Intent it1=new Intent(this,MyService.class);stopService(it1);break;case R.id.bind://绑定服务:最大作用是对Service执行的任务进行监控,如下载到哪了,音乐播放到哪了Intent it2=new Intent(this,MyService.class);bindService(it2,conn,BIND_AUTO_CREATE);break;case R.id.unbind://要指明解绑哪个连接,所以conn要设置为全局unbindService(conn);break;}}}

app MyService.java

package com.hala.srevicedemo;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;//实现进度监控(两个要素)://ServicerConnection:用与绑定客户端和Service->见MainActivity.java有说明//IBinder:在android中用于远程操作对象的一个基本接口//因为IBinder强制要求我们实现一些方法,而Binder类是给所有强制方法实现了一个空方法//在开发中我们会自己写一个内部类,继承Binder类,在里边写自己的方法public class MyService extends Service {public static final String TAG = "TAG";private int i;public MyService() {}/*** 创建*/@Overridepublic void onCreate() {super.onCreate();Log.e(TAG,"服务创建了");//开启一个线程,模拟耗时任务new Thread(){@Overridepublic void run() {super.run();try {for(i = 0; i <100; i++){sleep(1000);}} catch (InterruptedException e) {e.printStackTrace();}}}.start();}/*** 启动* @param intent* @param flags* @param startId* @return*/@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.e(TAG,"服务启动了");return super.onStartCommand(intent, flags, startId);}/*** 绑定* @param intent* @return*/@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.Log.e(TAG,"服务绑定了");return new IMyAidlInterface.Stub() {@Overridepublic void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {}@Overridepublic void showProgress() throws RemoteException {Log.d("TAG","当前进度是:"+i);}};//Binder操作返回值//return new MyBinder();}/*关于Binder操作自定义操作class MyBinder extends Binder{public int getProcess(){return i;}}*//*** 解绑* @param intent* @return*/@Overridepublic boolean onUnbind(Intent intent) {Log.e(TAG,"服务解绑了");return super.onUnbind(intent);}/*** 销毁*/@Overridepublic void onDestroy() {super.onDestroy();Log.e(TAG,"服务销毁了");}}

⚠️创建aidl文件完成后,要重新build,生成java文件,因为真正起作用的是这个java文件,java文件在哪里查看呢?

转换为工程视图,如下下图所示找到那个文件

app aidl文件 IMyAidlInterface.aidl

// IMyAidlInterface.aidlpackage com.hala.srevicedemo;// Declare any non-default types here with import statementsinterface IMyAidlInterface {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString);//定义自己所需要的方法void showProgress();}

aidldemo 布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.hala.aidldemo.MainActivity"><Button android:id="@+id/start"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="远程启动服务"android:onClick="onClick"/><Button android:id="@+id/stop"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="远程停止服务"android:onClick="onClick"/><Button android:id="@+id/bind"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="远程绑定服务"android:onClick="onClick"/><Button android:id="@+id/unbind"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="远程解锁服务"android:onClick="onClick"/></LinearLayout>

在工程视图下如下图复制aidl到要建立远程联系的app,注意同样需要重新build

aidldemo MainActivity.java

package com.hala.aidldemo;import ponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import com.hala.srevicedemo.IMyAidlInterface;public class MainActivity extends AppCompatActivity {ServiceConnection conn=new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//使用了aidlIMyAidlInterface imal=IMyAidlInterface.Stub.asInterface(service);try {imal.showProgress();} catch (RemoteException e) {e.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onClick(View view){switch(view.getId()){case R.id.start://Android 5.0后service的intent一定要显式声明Intent it=new Intent();//参数为Service起的别名it.setAction("com.hala.myservice");//参数为Service所在的包名it.setPackage("com.hala.srevicedemo");startService(it);break;case R.id.stop://Android 5.0后service的intent一定要显式声明Intent it1=new Intent();it1.setAction("com.hala.myservice");it1.setPackage("com.hala.srevicedemo");stopService(it1);break;case R.id.bind://Android 5.0后service的intent一定要显式声明Intent it2=new Intent();it2.setAction("com.hala.myservice");it2.setPackage("com.hala.srevicedemo");bindService(it2,conn,BIND_AUTO_CREATE);break;case R.id.unbind:unbindService(conn);break;}}}

显示结果

aidldemo

app

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。