最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Messenger實現(xiàn)進程間雙向通信

 更新時間:2021年05月21日 10:09:48   作者:Jason_Flash  
這篇文章主要為大家詳細(xì)介紹了Messenger實現(xiàn)進程間雙向通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡介

Messenger是安卓進程間通信 (IPC) 最為簡單的方式,可以實現(xiàn)進程間雙向通信。詳見官網(wǎng)介紹

代碼實現(xiàn)

服務(wù)端應(yīng)用實現(xiàn)

MessengerService接收客戶端發(fā)送的消息:

package com.test.messengerservice;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import androidx.annotation.NonNull;

public class MessengerService extends Service {

    //接收客戶端的消息類型
    private static final int SEND_MESSENGER = 0;
    private static final int CONFIG_NET = 1;
    private static final int CANCEL = 2;

    //發(fā)送給客戶端的消息類型
    private static final int FIND_DEVICE = 10;

    public MessengerService() {
    }

    private Messenger messenger = new Messenger(new ServiceHandler());
    private static Messenger mClient;

    public class ServiceHandler extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            // 處理消息
            switch(msg.what){
                case SEND_MESSENGER:
                    Log.d("service", "receive messenger");
                    mClient = msg.replyTo;
                    break;
                case CONFIG_NET:
                    Log.d("service", "config net task");
                    mClient = msg.replyTo;
                    break;
                case CANCEL:
                    Log.d("service", "cancel task");
                    mClient = msg.replyTo;
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("service", "service bind");
        return messenger.getBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("service", "service unbind");
        mClient = null;
        return super.onUnbind(intent);
    }

 //向客戶端發(fā)送消息
    public static void sendMessage() {
        if (null == mClient) {
            Log.d("service", "client is null");
            return;
        }
        try {
            Message message = Message.obtain(null, FIND_DEVICE);
            mClient.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

AndroidManifest.xml中注冊messenger服務(wù):

<service
    android:name=".MessengerService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MESSENGER"/>
    </intent-filter>
</service>

MainActivity中設(shè)置按鈕用于向客戶端主動發(fā)送消息:

package com.test.messengerservice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.findDeviceButton);
        button.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MessengerService.sendMessage();
            }
        });
    }
}

客戶端應(yīng)用實現(xiàn)

MainActivity中綁定服務(wù)端的service,并設(shè)置向客戶端發(fā)送消息的按鈕:

package com.test.messengerclient;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
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.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    // 服務(wù)端的Messenger
    private Messenger mService;
    // 客戶端的Messenger
    private Messenger mMessenger;

    private Button buttonConfigNet;
    private Button buttonCancel;

    //發(fā)送給服務(wù)端的消息類型
    private static final int SEND_MESSENGER = 0;
    private static final int CONFIG_NET = 1;
    private static final int CANCEL = 2;

    //接收服務(wù)端的消息類型
    private static final int FIND_DEVICE = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //客戶端Messenger
        mMessenger = new Messenger(new ClientHandler());
        //綁定服務(wù)
        bindServiceInvoked();

  //設(shè)置點擊事件
        buttonConfigNet = findViewById(R.id.buttonConfigNet);
        buttonCancel = findViewById(R.id.buttonCancel);
        
        buttonConfigNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mService != null) {
                    try {
                        Log.d("client", "send config net");
                        Message message = Message.obtain(null, CONFIG_NET);
                        message.replyTo = mMessenger;
                        mService.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mService != null) {
                    try {
                        Log.d("client", "send cancel");
                        Message message = Message.obtain(null, CANCEL);
                        message.replyTo = mMessenger;
                        mService.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

 //接收服務(wù)連接和斷開消息
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("client", "service connected");
            mService = new Messenger(service);

   //由于綁定后服務(wù)端沒有客戶端的Messenger ,綁定后先將客戶端Messenger發(fā)送給服務(wù)端
            if(mService != null) {
                try {
                    Log.d("client", "send messenger");
                    Message message = Message.obtain(null, SEND_MESSENGER);
                    message.replyTo = mMessenger;
                    mService.send(message);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("client", "service disconnected");
            mService = null;
        }
    };

 //從服務(wù)端接收消息
    public class ClientHandler extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case FIND_DEVICE:
                    Log.i("client", "find device");
                    break;
            }
        }
    }

 //綁定服務(wù)端的service
    private void bindServiceInvoked()
    {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.MESSENGER");
        intent.setPackage("com.test.messengerservice");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案

    Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案

    最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享
    2013-01-01
  • PowerManagerService之自動滅屏流程解析

    PowerManagerService之自動滅屏流程解析

    這篇文章主要為大家介紹了PowerManagerService之自動滅屏流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Flutter利用Hero組件實現(xiàn)自定義路徑效果的動畫

    Flutter利用Hero組件實現(xiàn)自定義路徑效果的動畫

    本篇介紹了如何利用Hero動畫組件的createRectTween屬性實現(xiàn)自定義路徑效果的動畫。文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-06-06
  • Kotlin擴展函數(shù)超詳細(xì)介紹

    Kotlin擴展函數(shù)超詳細(xì)介紹

    Kotlin?可以為一個不能修改的或來自第三方庫中的類編寫一個新的函數(shù)。?這個新增的函數(shù)就像那個原始類本來就有的函數(shù)一樣,可以用普通的方法調(diào)用,這種機制的函數(shù)稱為擴展函數(shù)
    2022-09-09
  • Android基于高德地圖poi的仿微信獲取位置功能實例代碼

    Android基于高德地圖poi的仿微信獲取位置功能實例代碼

    這篇文章主要介紹了Android基于高德地圖poi的仿微信獲取位置功能,當(dāng)用戶打開頁面自動定位,同時搜索周邊所有poi,點擊搜索按鈕輸入關(guān)鍵字,獲取關(guān)鍵字搜索結(jié)果,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2017-12-12
  • Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片

    Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片

    本篇文章主要介紹了Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • android開發(fā)權(quán)限詢問的示例代碼

    android開發(fā)權(quán)限詢問的示例代碼

    這篇文章主要介紹了android開發(fā)權(quán)限詢問的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android的Touch事件處理機制介紹

    Android的Touch事件處理機制介紹

    Android的Touch事件處理機制比較復(fù)雜,特別是在考慮了多點觸摸以及事件攔截之后,有需求的朋友可以參考下
    2012-11-11
  • 基于Android自定義控件實現(xiàn)刮刮樂效果

    基于Android自定義控件實現(xiàn)刮刮樂效果

    這篇文章主要介紹了基于Android自定義控件實現(xiàn)刮刮樂效果 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android編程之文件的讀寫實例詳解

    Android編程之文件的讀寫實例詳解

    這篇文章主要介紹了Android編程之文件的讀寫方法,結(jié)合實例形式較為詳細(xì)的分析了Android針對文件操作的詳細(xì)步驟,常用函數(shù)及使用技巧,需要的朋友可以參考下
    2015-12-12

最新評論

彭阳县| 永城市| 什邡市| 景洪市| 十堰市| 壤塘县| 公主岭市| 阿坝| 凤山县| 将乐县| 张北县| 化州市| 柏乡县| 湘潭市| 司法| 巨野县| 彩票| 广平县| 南宁市| 怀集县| 永福县| 孟州市| 扎囊县| 新疆| 郑州市| 宁乡县| 墨玉县| 正宁县| 兴和县| 建德市| 旌德县| 垫江县| 宜良县| 台中市| 和平区| 武清区| 赤水市| 修文县| 香格里拉县| 玛纳斯县| 石景山区|