您好社區我有以下問題。Android服務綁定問題
我的清單文件如下所示。
<?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);
我的Activity還具有以下私有類。
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。
有人可以指點我的錯誤,我似乎無法弄清楚。請原諒非常詳細的代碼,因爲我試圖儘可能明確地調試此問題。
感謝,
謝謝你的回答。但是,我確實需要公開服務中的一些方法。無論如何,謝謝你。 – DoubleDunk