1

我的 android 应用程序中有一项服务:

<service android:name="com.example.my.service.MyService" >
</service>

我在 build.gradle 中定义了两种风格:

defaultConfig {
    applicationId "com.example.my"
}
productFlavors {
    pro {

    }
    free {
        applicationId = "com.example.my.free"
    }
}

在com.example.my.MainActivity的onCreate中,我绑定了服务:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.my", "com.example.my.service.MyService");
bindService(intent, mMyServiceconnection, BIND_AUTO_CREATE);

pro 应用程序可以成功运行并绑定服务。但是当免费应用尝试绑定服务时,出现错误:

java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.example.my/.service.MyService }

该服务只需要在应用程序内部访问,免费应用程序有什么方法可以绑定服务吗?

4

1 回答 1

0

将清单中的服务声明更改为:
<service android:name=".service.MyService" />

然后在 com.example.my.MainActivity 的 onCreate() 中使用以下代码绑定到服务:

Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
    bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
于 2016-03-30T09:56:05.987 回答