服务绑定SecurityException

时间:2017-12-12 17:24:14

标签: android

我有一个有活动和服务的应用程序A.

我有另一个应用程序B绑定到服务并从服务访问公共方法。 当我尝试从应用程序B绑定服务时,它会出现以下错误。

由于这是一个安全例外,它是否与签署应用程序有关?

服务:

    public class CalculatorService extends Service {

    public CalculatorService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    ICalculatorService.Stub mBinder = new ICalculatorService.Stub()

    {

        @Override
        public int add(int a, int b) throws RemoteException {
            return a+b;
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    };
}

清单:

 <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=".Services.CalculatorService"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="com.example.xxx.aidltest.Services.ICalculatorService" />
            </intent-filter>
        </service>
    </application>

从其他应用程序调用服务:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ConnectAidlService();
        }

        ServiceConnection mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                calcInterface = ICalculatorService.Stub.asInterface(iBinder);
                try {
                    Log.d("Amol","Addition="+calcInterface.add(6,7));
                } catch(RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };

        public void ConnectAidlService(){
            Intent i = new Intent(ICalculatorService.class.getName());
            i.setClassName("com.example.xxx.aidltest","com.example.xxx.aidltest.Services.CalculatorService");
            getApplicationContext().bindService(i,mConnection, Context.BIND_AUTO_CREATE);
        }

错误:

com.example.XXX.clientserviceapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.xxxclientserviceapp, PID: 22447
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xxx.clientserviceapp/com.example.xxx.clientserviceapp.MainActivity}: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.example.xxx.clientserviceapp.ICalculatorService cmp=com.example.xxx.aidltest/.Services.CalculatorService }
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
                                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                                                      Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.example.xxx.clientserviceapp.ICalculatorService cmp=com.example.xxx.aidltest/.Services.CalculatorService }
                                                                                         at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1468)
                                                                                         at android.app.ContextImpl.bindService(ContextImpl.java:1422)
                                                                                         at android.content.ContextWrapper.bindService(ContextWrapper.java:636)
                                                                                         at com.example.xxx.clientserviceapp.MainActivity.ConnectAidlService(MainActivity.java:45)
                                                                                         at com.example.xxx.clientserviceapp.MainActivity.onCreate(MainActivity.java:21)
                                                                                         at android.app.Activity.performCreate(Activity.java:6723)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
                                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6123) 

1 个答案:

答案 0 :(得分:0)

我在代码中发现了错误。 我复制到客户端应用程序的AIDL文件包名不正确。

从开发者网站阅读以下内容后实现错误:

  

调用您在界面上定义的方法。你应该永远   陷阱DeadObjectException异常,当抛出时抛出   连接已经破裂。您还应该捕获SecurityException   异常,当涉及的两个进程被抛出时抛出   IPC方法调用具有冲突的AIDL定义。