Activity和Service 使用Binder进行通信 涉及到2种情况,一种是跨进程,一种是不跨进程的.
1.Activity和Service 使用Binder进行跨进程通信
其实这个可以参考之前已经记录的2篇文章,已经解释的很具体了,不做说明.
2.Activity和Service 使用Binder进行非跨进程的通信的一种方式
其实Activity和Service 之间想要通信还有其他方式,像通过广播也可以,需要传递数据的话可以使用Bundle 去传递.但是要注意Bundle 只能简单的基础类型的数据和实现了Parcelable的数据类型.
只所以记录这种方式是因为今天犯了一个错误,在跨进程的时候也使用类似下面的方法,结果就是出现以下报错:
11-15 15:42:02.943 29285 29285 E AndroidRuntime: java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.example.binder.DownloadServie$DownloadBinder
具体的demo记录如下:
public class DownloadActivity extends Activity {
private final static String TAG = "DownloadActivity";
private MyServiceConnection myServiceConnection = null;
private ProgressBar progress_bar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.binder_layout);
progress_bar = (ProgressBar) this.findViewById(R.id.progress_bar);
Intent intent = new Intent(this,DownloadServie.class);
myServiceConnection = new MyServiceConnection();
bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
unbindService(myServiceConnection);
}
public class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG,"onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG,"onServiceConnected");
DownloadServie downloadServie = ((DownloadServie.DownloadBinder)service).getDownloadServie();
downloadServie.setCallBack(new IProcessCallBack() {
@Override
public void setProcess(int i) {
progress_bar.setProgress(i);
}
});
downloadServie.download();
}
}
public interface IProcessCallBack{
public void setProcess(int i);
}
}
public class DownloadServie extends Service {
private final static String TAG = "DownloadServie";
private int i = 0;
private IProcessCallBack mCallBack;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"DownloadServie Thread Name:" + Thread.currentThread().getName() );
return new DownloadBinder();
}
public void download(){
new Thread(new Runnable() {
@Override
public void run() {
while(i <= 10){
Log.d(TAG,"download " + Thread.currentThread().getName() + " count" + i++);
try {
Thread.sleep(1000);
if(mCallBack != null){
mCallBack.setProcess(i*10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void setCallBack(IProcessCallBack callBack){
mCallBack = callBack;
}
class DownloadBinder extends Binder{
public DownloadServie getDownloadServie(){
return DownloadServie.this;
}
}
}
AndroidManifest.xml
<activity
android:name="com.example.binder.DownloadActivity"
android:label="@string/binder_download_test_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="com.example.binder.DownloadServie">