1、build中版本号为30及以上时,aidl无效,解决方案
①在客户端的manifest.xml中添加一下代码,其中代码中的包名为服务端的包名
< manifest>
. . .
< application>
. . . .
< / application>
< queries >
< package android : name= "com.example.clientapplication" / >
< intent>
< action android: name= "android.intent.action.MService" / >
< / intent>
< / queries>
< / manifest>
②修改build中的版本号
2、打开aidl中服务端的服务service出现闪退的问题
Caused by:
java. lang. RuntimeException: Didn 't create service "XXX" on path:
DexPathList [ [ zip file "/data/app/com.chemao.certification-2/base.apk" ] ,
nativeLibraryDirectories= [ / data/ app/ com. chemao. certification- 2 / lib/ arm, / vendor/ lib, / system/ lib] ]
方法:service的位置放错了,service应该放在java目录下。
aidl中服务端的目录结果如下所示:
3、跨进程通信aidl最简单的方法
①服务端
(1)AS切换模式为project,对main右键创建aidl文件,如下图:
(2)在创建的文件中定义想实现的接口
(3)build projection
如果rebuild出现问题的话,可以先clean projection
(4)创建类继承service,在service中创建内部类实现aidl中定义的接口数据,在java目录下创建,不要在aidl中进行创建,不然后续会出现问题。
public class MService extends Service {
private IAidlInterface mBinder;
@Nullable
@Override
public IBinder onBind ( Intent intent) {
Log . d ( "TAG" , "onBind: " + intent) ;
return mBinder. asBinder ( ) ;
}
@Override
public void onCreate ( ) {
super . onCreate ( ) ;
Log . d ( "TAG" , "onCreate: " ) ;
mBinder = new Binder ( ) ;
}
private class Binder extends IAidlInterface. Stub {
private static final String TAG = "mBinder" ;
@Override
public void basicTypes ( ) throws RemoteException {
Log . d ( TAG , "basicTypes: " + "我是服务端数据信息" ) ;
}
}
}
(5)在manifest中定义service,不然service无作用,在定义service中,可以声明action进行隐式调用
到此为止,服务端的工作完成
②客户端
(1)在manifest中进行安全防护
②将服务端中aidl复制过来,和服务端一模一样,进行rebuild即可。
(3)在activity中进行服务的绑定,bindservice返回值为true,onServiceConnected中有打印,就知道服务成功。
public class MainActivity extends AppCompatActivity {
private IAidlInterface mIExtraAidlInterface;
private ServiceConnection mServiceConn;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
Button button = findViewById ( R . id. name) ;
mServiceConn = new ServiceConnection ( ) {
@Override
public void onServiceConnected ( ComponentName name, IBinder service) {
mIExtraAidlInterface = IAidlInterface. Stub . asInterface ( service) ;
try {
mIExtraAidlInterface. basicTypes ( ) ;
} catch ( RemoteException e) {
throw new RuntimeException ( e) ;
}
Log . d ( TAG , "onServiceConnected: " ) ;
}
@Override
public void onServiceDisconnected ( ComponentName name) {
Toast . makeText ( MainActivity . this , "0000" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . d ( TAG , "onServiceDisconnected: " ) ;
}
} ;
bindService ( ) ;
}
private void bindService ( ) {
Intent intent = new Intent ( ) ;
intent. setAction ( "android.intent.action.MService" ) ;
intent. setPackage ( "com.example.clientapplication" ) ;
ResolveInfo resolveInfo = getPackageManager ( ) . resolveService ( intent, 0 ) ;
Log . d ( TAG , "bindService: " + resolveInfo) ;
bindService ( intent, mServiceConn, BIND_AUTO_CREATE ) ;
Log . d ( TAG , "mServiceConn: " + mServiceConn) ;
}
@Override
public boolean bindService ( Intent service, ServiceConnection conn, int flags) {
Log . d ( TAG , "bindService: service " + service + "conn " + conn + "flags " + flags) ;
Log . d ( TAG , "bindService: " + super . bindService ( service, conn, flags) ) ;
return super . bindService ( service, conn, flags) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
unbindService ( mServiceConn) ;
}
}