0

我正在开发一个从 API 8 到 API 16 的应用程序。

在启动时,该程序应该绑定到我的本地服务。

在使用 API 16 设置测试 AVD 时,我没有遇到服务绑定或其他任何问题。

在使用 API 8 设置对 AVD 进行测试时,它根本不会绑定到我的本地服务。

这是我的日志:

11-15 10:35:57.220: E/AndroidRuntime(326): FATAL EXCEPTION: main
11-15 10:35:57.220: E/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{timesheetdb.Login/timesheetdb.Login.TimesheetConnect}: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=timesheetdb.Login/timesheetdb.Services.SocketService }
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.os.Looper.loop(Looper.java:123)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 10:35:57.220: E/AndroidRuntime(326):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 10:35:57.220: E/AndroidRuntime(326):  at java.lang.reflect.Method.invoke(Method.java:521)
11-15 10:35:57.220: E/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 10:35:57.220: E/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 10:35:57.220: E/AndroidRuntime(326):  at dalvik.system.NativeStart.main(Native Method)
11-15 10:35:57.220: E/AndroidRuntime(326): Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=timesheetdb.Login/timesheetdb.Services.SocketService }
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ContextImpl.bindService(ContextImpl.java:874)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
11-15 10:35:57.220: E/AndroidRuntime(326):  at timesheetdb.Login.TimesheetConnect.onStart(TimesheetConnect.java:47)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.Activity.performStart(Activity.java:3781)
11-15 10:35:57.220: E/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
11-15 10:35:57.220: E/AndroidRuntime(326):  ... 11 more

我不明白的是为什么会有一个 SecurityException 说我不允许绑定到服务。我可以在 API 16 中绑定它,但我不能在 API 8 中绑定。这不合逻辑。

请问,有人可以帮忙吗?

根据要求:这不是整个代码,只是在活动开始时处理绑定的部分。

private SocketService mainService;
private ServiceConnection serviceConn;
private boolean isServiceBound;

@Override
protected void onStart() {
    super.onStart();

    // First create ServiceConnection
    createServiceConnection();

    // Bind to SocketService
    Intent sInt = new Intent(this, SocketService.class);
    if ((isServiceBound = bindService(sInt, this.serviceConn,
            Context.BIND_AUTO_CREATE))) {
        Log.i("info", "Service has been bound.");
    }
}

@Override
protected void onStop() {
    super.onStop();

    if (isServiceBound) {
        unbindService(serviceConn);
        isServiceBound = false;
    }
}

@Override
protected void onDestroy(){
    super.onDestroy();

    if (isServiceBound) {
        unbindService(serviceConn);
        isServiceBound = false;
    }
}

private void createServiceConnection() {
    this.serviceConn = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
            mainService = null;
            isServiceBound = false;
            Log.i("info", "Service disconnected.");
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            mainService = ((SocketService.SocketBinder) service)
                    .getService();
            isServiceBound = true;
            Log.i("info", "Service connected.");
        }
    };
}

并且服务的 Manifest 定义是:

  <service
        android:name="timesheetdb.Services.SocketService"
        android:exported="false"
        android:permission="normal" android:stopWithTask="true" android:enabled="true">
        <intent-filter>
            <action android:name="timesheetdb.Services.SocketService" />
        </intent-filter>
    </service>
4

2 回答 2

0

使用 google api(api 8)尝试用户模拟器

于 2012-11-15T16:31:12.510 回答
0

这是服务定义的有效属性列表

http://developer.android.com/guide/topics/manifest/service-element.html

您可以尝试从清单中的服务定义中删除权限和 stopWithTask 属性。

让我知道这是否有效

于 2012-11-15T17:31:08.390 回答