我正在开发一个应用程序,将从API 8到API 16工作。
在启动时,程序应该绑定到我的本地服务。
在使用API16设置的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.");
}
};
}服务的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>发布于 2012-11-16 01:31:08
以下是服务定义的有效属性列表
http://developer.android.com/guide/topics/manifest/service-element.html您可以尝试从清单中的服务定义中删除permission和stopWithTask属性。
如果有效,请让我知道
发布于 2012-11-16 00:31:13
使用google的试用用户模拟器(API8)
https://stackoverflow.com/questions/13401763
复制相似问题