0

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 ではバインドできません。論理的ではありません。

誰か助けてください。

リクエストに応じて: コード全体ではなく、アクティビティの開始時にバインディングを処理する部分のみです。

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

マニフェストのサービス定義から、permission 属性と stopWithTask 属性を削除してみてください。

それが機能するかどうか教えてください

于 2012-11-15T17:31:08.390 に答える