问题描述
我使用的是 Android Automotive 11 环境,以下代码实际上也无法在常规 Android 11 环境下运行。
我使用我从源 Android 11 r35 构建的模拟器。
当我尝试打开 AIDL 服务时出现以下错误:
权限监视器:NameNotFoundException
代码如下:
public class MySimpService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new SimpServiceImp();
}
}
SimpServiceImp 类:
public class SimpServiceImp extends ISimp.Stub {
@Override
public int add(int a,int b) throws remoteexception {
return a+b;
}
@Override
public int sub(int a,int b) throws remoteexception {
return a-b;
}
}
AndroidManifest.xml
<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/Theme.AIDLService" >
<service android:name=".MySimpService">
<intent-filter>
<action android:name="com.example.aidlservice.MySimpService"/>
</intent-filter>
</service>
</application>
解决方法
有很多基本的 Android 问题导致它无法正常工作。 部分原因是服务必须有通知才能保持活动状态。
https://github.com/NewstHet/AndroidAidlClient https://github.com/NewstHet/AndroidAIDLService
Github 链接包含使用 Android 11 API 30 的有效 AIDL 服务。