I am developing an app that will work from API 8 through API 16.
At start up, the program is supposed to bind to my local service.
When testing on an AVD with settings for API 16, I have had no issues, with service binding or anything else.
When testing on an AVD with settings for API 8, it will not bind to my local service at all.
Here is my 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
What I do not understand, is why there is a SecurityException saying I am not allowed to bind to the service. I can bind to it in API 16, but I cannot in API 8. It’s not logical.
Please, can someone help?
As per request:
It’s not the entire code, just the pieces that handle the binding when the Activity begins.
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.");
}
};
}
And the Manifest definition of the service is:
<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>
Here is a list of valid attributes for a service defintion
You can try remove the permission and stopWithTask attributes from your service definition in the manifest.
Let me know if that works