服务已经启动时,bindservice失败

时间:2012-11-16 10:02:15

标签: android android-intent android-service

我从app 1开始提供服务。此应用程序清单中的服务声明如下:

     <service android:name="jp.co.xyz.bluetooth.profile.TIPServer"
     android:permission="jp.co.abc.android.OMRSSettings.permission.Access" >
<intent-filter>
    <action android:name="jp.co.xyz.bluetooth.api.ICommonResultCallback" />
    <action android:name="jp.co.xyz.bluetooth.api.ITimeServer" />
</intent-filter>

我尝试从第二个应用绑定到此服务。第二个应用程序清单中的服务声明如下:

 <service android:name="jp.co.xyz.bluetooth.profile.TIPServer"
     android:permission="jp.co.abc.android.OMRSSettings.permission.Access" >
<intent-filter>
    <action android:name="jp.co.xyz.bluetooth.api.ICommonResultCallback" />
    <action android:name="jp.co.xyz.bluetooth.api.ITimeServer" />
</intent-filter>

以下是我用来从app 2进行bindservice的代码。

Intent time_intent = new Intent(ITimeServer.class.getName());
mContext.bindService(time_intent, time_connection, Context.BIND_AUTO_CREATE);

当我尝试绑定到已经从app 1启动的服务时,我收到以下异常。

01-02 00:06:54.531: INFO/PowerManagerService(425): Start Light.setBrightness(), [20],  [3]
01-02 00:06:56.473: INFO/PowerManagerService(425): Start Light.setBrightness(), [130], [3]
01-02 00:06:58.055: WARN/dalvikvm(4956): threadid=1: thread exiting with uncaught exception (group=0x40b70390)
01-02 00:06:58.055: WARN/ActivityManager(425): Permission Denial: Accessing service ComponentInfo{jp.co.abc.android.omrsettings/jp.co.xyz.bluetooth.profile.TIPServer} from   pid=4956, uid=10158 requires jp.co.abc.android.OMRSettings.permission.Access
01-02 00:06:58.065: ERROR/AndroidRuntime(4956): FATAL EXCEPTION: main
java.lang.SecurityException: Not allowed to bind to service Intent { act=jp.co.xyz.bluetooth.api.ITimeServer }
at android.app.ContextImpl.bindService(ContextImpl.java:1187)
at android.content.ContextWrapper.bindService(ContextWrapper.java:370)
at jp.co.abc.middleware.tip.LeTimeServerProfile.startTimeServer(LeTimeServerProfile.java:45)
at jp.co.abc.tip.TimeActivity.onClick(TimeActivity.java:49)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14133)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
01-02 00:06:58.095: WARN/ActivityManager(425): Force finishing activity  jp.co.abc.tip/.TimeActivity

当我删除app 1时,我会毫无问题地绑定到服务。

有人可以告诉我这个问题的原因和解决方案吗?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

你错过了在你的清单中编写android:process =“:remote”... 你的声明应该是

 <service android:name="jp.co.xyz.bluetooth.profile.TIPServer" android:process=":remote">

android:process =“:remote”属性告诉Android在与应用程序其余部分分开的进程中运行我们的服务

答案 1 :(得分:0)

使用此代码:

Intent intent = new Intent();
intent.setAction("com.action");
intent.setPackage("com.package.to.service");