你好社区我有以下问题。
我的清单文件如下所示。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.someCo.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.wifi" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<service android:name = ".BackgroundService" android:exported="true" />
</application>
</manifest>
我的 Activity 的 onCreate 具有以下代码行。
this.context.startService(new Intent(this.context, com.someCo.test.BackgroundService.class));
this.context.bindService(new Intent(this.context, com.someCo.test.BackgroundService.class), serviceConnection, Context.BIND_AUTO_CREATE);
我的活动也有以下私人课程。
private ServiceConnection serviceConnection = new ServiceConnection() {
public static final String TAG = "Service Connection Class";
public void onServiceConnected(ComponentName className, IBinder service) {
try {
com.someCo.test.BackgroundService.MyBinder binder = (com.someCo.test.BackgroundService.MyBinder)service;
backgroundService = binder.getService();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//backgroundService = ((BackgroundService.MyBinder) service).getService();
//Toast.makeText(context,"service connected", Toast.LENGTH_LONG);
Log.i(TAG, "onServiceConnected");
}
//@Override
public void onServiceDisconnected(ComponentName className) {
backgroundService = null;
}
};
我的服务有以下内容。
private final IBinder binder = new MyBinder();
public class MyBinder extends Binder{
private static final String TAG = "MyBinder";
BackgroundService getService(){
Log.i(TAG, "get service");
return BackgroundService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onBind destroyed");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind destroyed");
return super.onUnbind(intent);
}
在这一行,我得到了代码下方的异常。
(com.someCo.test.BackgroundService.MyBinder)service;
backgroundService = binder.getService();
java.lang.classCastException:android.os.BinderProxy。
有人可以指出我的错误,我似乎无法弄清楚。请原谅非常冗长的代码,因为我试图在调试这个问题时尽可能明确。
谢谢,