可以绑定到API 16(4.1.2)中的本地服务,但不能绑定到API 8中的本地服务(2.2)

时间:2012-11-15 16:21:45

标签: android binding service

我正在开发一个可以在API 8到API 16下运行的应用程序。

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

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

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

这是我的logcat:

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中。它不合逻辑。

拜托,有人可以帮忙吗?

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

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.");
        }
    };
}

服务的清单定义是:

  <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>

2 个答案:

答案 0 :(得分:0)

使用google api(api 8)

尝试用户模拟器

答案 1 :(得分:0)

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

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

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

如果有效,请告诉我

相关问题