- DemoA
- DemoB
前提:必须先启动DemoB的Service,DemoA与DemoB成功进行IPC。
运行结果
2021-09-12 13:52:51.986 2568-2568/com.jlink.demob D/wangrui: 通信管道正在绑定 2021-09-12 13:53:11.969 2568-2568/com.jlink.demob D/wangrui: 接收到客户端消息 2021-09-12 13:53:11.970 2568-2568/com.jlink.demob D/wangrui: 名字:王睿 年龄21DemoA
MainActivity.java
public class MainActivity extends AppCompatActivity { private Button btnIPC; private Button btnSend; private Messenger messengerProxy; private Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); Bundle bundle = msg.getData(); bundle.setClassLoader(MyBean.class.getClassLoader()); MyBean myBean = bundle.getParcelable("message"); handler.postDelayed(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,myBean.getName(),Toast.LENGTH_SHORT).show(); } },3000); } }; private Messenger clientMessenger = new Messenger(handler); private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { messengerProxy = new Messenger(binder); Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName name) { android.util.Log.d("wangrui","断开连接!"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnIPC = findViewById(R.id.btn_ipc); btnIPC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intentService = new Intent(); intentService.setAction("com.jlink.demob.MyService"); intentService.setPackage("com.jlink.demob"); intentService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainActivity.this.bindService(intentService, mServiceConnection, BIND_AUTO_CREATE); } }); btnSend = findViewById(R.id.btn_send); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // MyBean myBean = new MyBean(); // myBean.setName("王睿对皮卡丘使用了精灵球"); try { Message message = new Message(); // message.replyTo = clientMessenger; Bundle bundle = new Bundle(); bundle.putString("name", "王睿"); bundle.putString("age","21"); // bundle.putParcelable("message",myBean); message.setData(bundle); messengerProxy.send(message); } catch (RemoteException e) { e.printStackTrace(); } } }); } }
MyService.java
public class MyService extends Service { private Handler handler = new Handler(Looper.getMainLooper()); private Messenger messenger = new Messenger(handler); @Nullable @Override public IBinder onBind(Intent intent) { android.util.Log.d("wangrui","通信管道正在绑定"); return messenger.getBinder(); } }
AndroidManifest.xml
DemoB
MainActivity.java
public class MainActivity extends AppCompatActivity { private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intentService = new Intent(); intentService.setAction("com.jlink.demob.MyService"); intentService.setPackage("com.jlink.demob"); intentService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainActivity.this.bindService(intentService, mServiceConnection, BIND_AUTO_CREATE); }
MyService.java
public class MyService extends Service { private Handler handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); android.util.Log.d("wangrui","接收到客户端消息"); // 客户端→服务端 Bundle bundle = msg.getData(); android.util.Log.d("wangrui","名字:" + bundle.getString("name")+"nn"+"年龄:" + bundle.getString("age")); // bundle.setClassLoader(MyBean.class.getClassLoader()); // MyBean myBean = bundle.getParcelable("message"); // Toast.makeText(MyService.this,myBean.getName(),Toast.LENGTH_SHORT).show(); // //服务端→客户端 // try { // Messenger clientMessenger = msg.replyTo; // myBean = new MyBean(); // myBean.setName("皮卡丘对王睿使用了十万伏特"); // bundle = new Bundle(); // bundle.putParcelable("message",myBean); // Message message = new Message(); // message.setData(bundle); // message.replyTo = clientMessenger; // clientMessenger.send(message); // } catch (RemoteException e) { // e.printStackTrace(); // } } }; private Messenger messenger = new Messenger(handler); @Nullable @Override public IBinder onBind(Intent intent) { android.util.Log.d("wangrui","通信管道正在绑定"); return messenger.getBinder(); } }
AndroidManifest.xml
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)