我每次聲明和運行兩個服務時遇到下面的binder.proxy異常。一個服務運行在不同的進程中(私有應用程序),另一個服務運行與My Application在運行Binder實現的(默認應用程序進程)中相同的進程。獲取java.lang.ClassCastException:android.os.BinderProxy每次我聲明並運行兩個服務
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service.check"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:name="com.service.check.MainApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.service.check.SecondService"
android:exported="false"/>
<service
android:name="com.service.check.FirstService"
android:process=":newProcess" >
</service>
</application>
</manifest>
我發動我的第一個服務於MainActivity的按鈕點擊爲:
MainActivity.java
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button mLanchServiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLanchServiceBtn=(Button) findViewById(R.id.launch_btn);
mLanchServiceBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//Starting first service
Intent launch=new Intent(this,FirstService.class);
startService(launch);
}
}
而在第二個服務MainApplication類爲。
MainApplication.java
public class MainApplication extends Application {
private SecondService.LocalBinder mBinder;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
mBinder = (LocalBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
@Override
public void onCreate() {
super.onCreate();
//starting second service
Intent launch=new Intent(this,SecondService.class);
startService(launch);
//Binding to it
bindService(launch, mConnection, BIND_AUTO_CREATE);
}
}
FirstService.java
public class FirstService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
SecondService.java
public class SecondService extends Service{
//Service Containing Local Binder
private LocalBinder mBinder=new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
class LocalBinder extends Binder{
public LocalBinder() {
}
}
}
堆棧跟蹤:
02-05 10:32:25.035: E/AndroidRuntime(1424): Process:
com.service.check:newProcess, PID: 1424 02-05 10:32:25.035:
E/AndroidRuntime(1424): java.lang.ClassCastException:
android.os.BinderProxy cannot be cast to
com.service.check.SecondService$LocalBinder 02-05 10:32:25.035:
E/AndroidRuntime(1424): at
com.service.check.MainApplication$1.onServiceConnected(MainApplication.java:23)
02-05 10:32:25.035: E/AndroidRuntime(1424): at
android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1101)
我已經提到了以下鏈接進行梳理它說, 如果我的活動和服務都在獨立的進程那麼我們不應該我做的方式結合的問題。
Android service android.os.BinderProxy error
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to LocalBinder
但在我的情況: 我從MainApplication結合SecondService並且都在同一個進程(即默認應用程序)運行。仍然我面臨binderProxy異常在SecondService,而我的FirstService運行在一個單獨的過程中,我甚至沒有綁定。
請幫助我解決這種情況,並建議我一個儘可能好的方法,以便我可以在沒有任何崩潰的情況下實現相同的方案。
您是否確認應用程序和服務在相同的過程中運行? – pskink 2015-02-06 11:44:51
我沒有在android設備監視器中明確驗證它,但通過日誌,我發現我在第二個服務中崩潰,它應該在與應用程序相同的進程中運行,因爲我沒有爲它分配任何新的進程表現。 – 2015-02-07 15:39:08
通常,如果一個IBinder是一個BinderProxy,這意味着另一方正在不同的過程中運行,所以... – pskink 2015-02-07 15:42:11