Use messenger to realize two-way communication of service

Time:2021-7-26

There is a solution in the Android architecture: use Android messenger to realize two-way communication between service processes.

Mainactivity class:

package com.example.messengeservicetest;
 
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.style.IconMarginSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
 private Messenger sender;
 
 private ServiceConnection conn;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button bind = (Button)findViewById(R.id.bind);
  Button start = (Button)findViewById(R.id.start);
  Button send = (Button)findViewById(R.id.send);
  
  bind.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    getStart();
   }
  });
  
  
  start.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, MessengerService.class);
    startService(intent);
   }
  });
  
  send.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    sendCount();
   }
  });
 }
 
 //Used to start the messengerservice
 private void getStart(){
  
  conn = new ServiceConnection() {
   
   @Override
   public void onServiceDisconnected(ComponentName name) {
    
   }
   
   @Override
   public void onServiceConnected(ComponentName name, IBinder service) {
    
    sender = new Messenger(service);
   }
  };
  
  
  Intent intent = new Intent(this, MessengerService.class);
  
//  startService(intent);
  bindService(intent, conn, Service.BIND_AUTO_CREATE);
 
 }
 
 //Transfer value
 private void sendCount(){
  
  Message msg = new Message();
  msg.arg1 = 20;
  msg.arg2 = 20;
 
  try {
   sender.send(msg);
  } catch (RemoteException e) {
   e.printStackTrace();
  }
  
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  unbindService(conn);
 }
 
}

Messengerservice.java class:


package com.example.messengeservicetest;
 
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.util.Log;
 
public class MessengerService extends Service {
 
 Messenger messenger;
 Handler handler;
 int a, b;
 int sum = 0;
 
 @Override
 public IBinder onBind(Intent intent) {
  return messenger.getBinder();
 }
 
 @Override
 public void onCreate() {
  super.onCreate();
  
  handler = new Handler(){
 
   @Override
   public void handleMessage(Message msg) {
    super.handleMessage(msg);
 
    a = msg.arg1;
    b = msg.arg2;
   }
   
  };
  messenger = new Messenger(handler);
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  
  int sum = subSum(a, b);
  System.out.println(sum + "");
  return super.onStartCommand(intent, flags, startId);
 }
 
 private int subSum(int a, int b){
  int sum = a + b;
  return sum;
 }
}

Register in androidmanifest.xml:


<service android:name=".MessengerService"></service>

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]