纯净、安全、绿色的下载网站

首页|软件分类|下载排行|最新软件|IT学院

当前位置:首页IT学院IT技术

Messenger双向通信 使用Messenger实现Service的双向通信

antimage08   2021-05-21 我要评论
想了解使用Messenger实现Service的双向通信的相关内容吗,antimage08在本文为您仔细讲解Messenger双向通信的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Messenger,Service,双向通信,下面大家一起来学习吧。

Android体系架构中有一个解决方案:利用Android Messenger实现Service进程间双向通信。

MainActivity 类:

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();
   }
  });
 }
 
 // 用于启动 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);
 
 }
 
 // 传递数值
 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 类:

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;
 }
}

在 AndroidManifest.xml 中注册:

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

相关文章

猜您喜欢

网友评论

Copyright 2020 www.moon-script.com 【月光下载】 版权所有 软件发布

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 点此查看联系方式