Activity和Service 使用Binder进行通信

本文深入探讨了Android中Activity与Service间使用Binder进行通信的方法,包括跨进程与非跨进程通信的具体实现细节,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activity和Service 使用Binder进行通信 涉及到2种情况,一种是跨进程,一种是不跨进程的.

1.Activity和Service 使用Binder进行跨进程通信

其实这个可以参考之前已经记录的2篇文章,已经解释的很具体了,不做说明.

IPC-AIDL的使用实例和分析

IPC-Messenger使用实例

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">

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值