我试了两种远程调用的方法,一种是使用AIDL;一种是直接使用class localBinder extends Binder.
两种方试都是从android文档里面照抄的。只是改变了下名字。
可是,我运行的时候:
使用AIDL方法 activity中的onServiceConnected()一直没有运行,bindService()也返回false.
使用localBinder方法,成功的执行了bindService()-> service.onCreate->service.onBind(),然后到这里就出问题了,报了一个classCastException: android.os.BinderProxy can not to cast ...localBinder.
快帮我看看是哪里的问题吧,都两天了。由于自己英文不太好,文档看了几遍也没有更多的能帮我打出问题的。
我看了 service下bindService()函数的文档,以及AIDL的文档。使用方法也是从例子里模仿来的。
注意下“//-----------------------------"的地方,如果想使用AIDL方法或localBinder方法需要对这几个地方进行更改。
下面是代码://TestAIDL.java 主activity
package Learn.testaidl;import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;public class TestAIDL extends Activity {
public final String TAG = "TestAIDL";
//AIDLserviceInterface mService = null; //-------------------------------
AIDLservice mService = null;
Button btnstart;
Button btnend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_aidl);
btnstart = (Button) findViewById(R.id.button1);
btnend = (Button) findViewById(R.id.button2);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(//------------------------------------------------
/*
* bindService(new Intent(AIDLserviceInterface.class.getName()), mConnection, Context.BIND_AUTO_CREATE))
*/
getApplicationContext().bindService(new Intent(TestAIDL.this, AIDLservice.class), mConnection, Context.BIND_AUTO_CREATE)
)
Log.i(TAG, "btnStart: bindService...");
else
Log.i(TAG, "btnStart: bindService failure");
}
});
btnend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "btnend: unbindService...");
getApplicationContext().unbindService(mConnection);
}
});
} public ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//-------------------------------------------------------------------------
/*
mService = AIDLserviceInterface.Stub.asInterface(service);
try {
mService.refreshService();
}catch(NullPointerException e) {
Log.i(TAG, "onServiceConnected: NullPointerException");
}catch(RemoteException e){
Log.i(TAG, "onServiceConnected: RemoteException");
}
*/
mService = ((AIDLservice.LocalBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_test_aidl, menu);
return true;
}}
//AIDLservice extends Service 服务类
package Learn.testaidl;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class AIDLservice extends Service{ public final String TAG = "DIALservice";
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
} @Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
} //-----------------------------------------------------
// There are two methods of connect IPC that onBind -> return mIBinder or mBinder_Stub.
public AIDLserviceInterface.Stub mBinder_Stub = new AIDLserviceInterface.Stub() {
@Override
public void refreshService() throws RemoteException {
Log.i(TAG, "Stub.refreshService");
}
};
public class LocalBinder extends Binder {
AIDLservice getService() {
return AIDLservice.this;
}
}
private final IBinder mIBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return mIBinder;
}
}<--!这里是mainfest.xml的内容, 另外,这个程序没有界面,全程使用log.i(tag,string)-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Learn.testaidl"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestAIDL"
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=".AIDLservice"
android:enabled="true"
android:process=":remote"
android:exported="true">
</service>
</application></manifest>//最后是 AIDLserviceInterface.aidl 文件
package Learn.testaidl;interface AIDLserviceInterface{
void refreshService();
}androidservicebindService
两种方试都是从android文档里面照抄的。只是改变了下名字。
可是,我运行的时候:
使用AIDL方法 activity中的onServiceConnected()一直没有运行,bindService()也返回false.
使用localBinder方法,成功的执行了bindService()-> service.onCreate->service.onBind(),然后到这里就出问题了,报了一个classCastException: android.os.BinderProxy can not to cast ...localBinder.
快帮我看看是哪里的问题吧,都两天了。由于自己英文不太好,文档看了几遍也没有更多的能帮我打出问题的。
我看了 service下bindService()函数的文档,以及AIDL的文档。使用方法也是从例子里模仿来的。
注意下“//-----------------------------"的地方,如果想使用AIDL方法或localBinder方法需要对这几个地方进行更改。
下面是代码://TestAIDL.java 主activity
package Learn.testaidl;import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;public class TestAIDL extends Activity {
public final String TAG = "TestAIDL";
//AIDLserviceInterface mService = null; //-------------------------------
AIDLservice mService = null;
Button btnstart;
Button btnend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_aidl);
btnstart = (Button) findViewById(R.id.button1);
btnend = (Button) findViewById(R.id.button2);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(//------------------------------------------------
/*
* bindService(new Intent(AIDLserviceInterface.class.getName()), mConnection, Context.BIND_AUTO_CREATE))
*/
getApplicationContext().bindService(new Intent(TestAIDL.this, AIDLservice.class), mConnection, Context.BIND_AUTO_CREATE)
)
Log.i(TAG, "btnStart: bindService...");
else
Log.i(TAG, "btnStart: bindService failure");
}
});
btnend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "btnend: unbindService...");
getApplicationContext().unbindService(mConnection);
}
});
} public ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//-------------------------------------------------------------------------
/*
mService = AIDLserviceInterface.Stub.asInterface(service);
try {
mService.refreshService();
}catch(NullPointerException e) {
Log.i(TAG, "onServiceConnected: NullPointerException");
}catch(RemoteException e){
Log.i(TAG, "onServiceConnected: RemoteException");
}
*/
mService = ((AIDLservice.LocalBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_test_aidl, menu);
return true;
}}
//AIDLservice extends Service 服务类
package Learn.testaidl;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class AIDLservice extends Service{ public final String TAG = "DIALservice";
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
} @Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
} //-----------------------------------------------------
// There are two methods of connect IPC that onBind -> return mIBinder or mBinder_Stub.
public AIDLserviceInterface.Stub mBinder_Stub = new AIDLserviceInterface.Stub() {
@Override
public void refreshService() throws RemoteException {
Log.i(TAG, "Stub.refreshService");
}
};
public class LocalBinder extends Binder {
AIDLservice getService() {
return AIDLservice.this;
}
}
private final IBinder mIBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return mIBinder;
}
}<--!这里是mainfest.xml的内容, 另外,这个程序没有界面,全程使用log.i(tag,string)-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Learn.testaidl"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestAIDL"
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=".AIDLservice"
android:enabled="true"
android:process=":remote"
android:exported="true">
</service>
</application></manifest>//最后是 AIDLserviceInterface.aidl 文件
package Learn.testaidl;interface AIDLserviceInterface{
void refreshService();
}androidservicebindService
package Learn.testaidl;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class AIDLservice extends Service{ public final String TAG = "DIALservice";
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
} @Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
} //-----------------------------------------------------
// There are two methods of connect IPC that onBind -> return mIBinder or mBinder_Stub.
public AIDLserviceInterface.Stub mBinder_Stub = new AIDLserviceInterface.Stub() {
@Override
public void refreshService() throws RemoteException {
Log.i(TAG, "Stub.refreshService");
}
};
// public class LocalBinder extends Binder {
// AIDLservice getService() {
// }
// }
//
// private final IBinder mIBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return mBinder_Stub;
}
}//TestAIDL.java 主activity
package Learn.testaidl;import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class TestAIDL extends Activity {
public final String TAG = "TestAIDL";
//AIDLserviceInterface mService = null; //-------------------------------
AIDLserviceInterface mService = null;
Button btnstart;
Button btnend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstart = (Button) findViewById(R.id.button1);
btnend = (Button) findViewById(R.id.button2);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(//------------------------------------------------
/*
* bindService(new Intent(AIDLserviceInterface.class.getName()), mConnection, Context.BIND_AUTO_CREATE))
*/
getApplicationContext().bindService(new Intent(TestAIDL.this, AIDLservice.class), mConnection, Context.BIND_AUTO_CREATE)
)
Log.i(TAG, "btnStart: bindService...");
else
Log.i(TAG, "btnStart: bindService failure");
}
});
btnend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "btnend: unbindService...");
getApplicationContext().unbindService(mConnection);
}
});
} public ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//-------------------------------------------------------------------------
mService = AIDLserviceInterface.Stub.asInterface(service);
try {
mService.refreshService();
}catch(NullPointerException e) {
Log.i(TAG, "onServiceConnected: NullPointerException");
}catch(RemoteException e){
Log.i(TAG, "onServiceConnected: RemoteException");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};}