android IPC方式 Bundle 文件共享 Messenger

1.添加进程 四大组件中 如android:process="android.services"

2.通信方式

 发送者:

 

// 1)使用Bundle跨进程通信
        Intent mIntent = new Intent(this, LActivity.class);
        mIntent.putExtra("name", " value");
         startActivity(mIntent);
        // 2)使用文件共享
        File file = new File("/sdcard/a.txt");
        ObjectOutputStream mOutputStream = null;
        try {
            mOutputStream = new ObjectOutputStream(new FileOutputStream(file));
            mOutputStream.writeObject(new UserEntity("勒布朗", "32"));
            mOutputStream.flush();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                mOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        startActivity(mIntent);
        // 3)使用Messenger
         mConnection = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Messenger messenger = new Messenger(service);
                sendMsg(messenger, 1, String.valueOf(2));
            }
        };
        bindService(new Intent(this, MyBindServices.class), mConnection, 1);

    }
    public void sendMsg(Messenger messenger,int what, String msg){ //这个方法就是自己的业务信息发送消息 传递到Service中 有 Handler处理。  
        if (messenger == null) {  
            return;  
        }  
        Message msg1 = Message.obtain();
        Bundle mBundle=new Bundle();
        mBundle.putString("key", "value");
        msg1.setData(mBundle);
        //注意:msg.obj這些不是Bundler不能傳送
        try {  
            messenger.send(msg1);  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }  
    }


接送者1:

//1)使用bundle通信
        mTextView.setText(getIntent().getStringExtra("name"));
         //2)使用文件共享
         File file=new File("/sdcard/a.txt");
         ObjectInputStream mInputStream = null;
         try {
             mInputStream=new ObjectInputStream(new FileInputStream(file));
             try {
                UserEntity mUserEntity=     (UserEntity)mInputStream.readObject();
                mTextView.setText(mUserEntity.toString());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                mInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


接送者2:


import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;

public class MyBindServices extends Service {
    Messenger message;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle mBundle = msg.getData();
            Toast.makeText(MyBindServices.this, mBundle.getString("key"), 1)
                    .show();
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        message = new Messenger(mHandler);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return message.getBinder();
    }

}


下载:IPC方式 Bundle 文件共享 Messenger以及aidl


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值