zoukankan      html  css  js  c++  java
  • android开发之使用Messenger实现service与activity交互

    service与activity交互的方式有多种,这里说说使用Messenger来实现两者之间的交互。

    Service程序

    public class MessengerService extends Service {
    
        final Messenger mMessenger = new Messenger(new IncomingHandler());
        @Override
        public IBinder onBind(Intent intent) {
            return mMessenger.getBinder();
        }
    
        @SuppressLint("HandlerLeak")
        class IncomingHandler extends Handler {
    
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 0:
                    Toast.makeText(MessengerService.this,"Hello lenve!", Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    Toast.makeText(MessengerService.this,"Hello lenve!Hello world!", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
                }
            }
    
        }
    }

    MainActivity.java

    public class MainActivity extends Activity {
    
        private Messenger mMessenger;
        private ServiceConnection conn;
        private Intent intent;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent = new Intent(this,MessengerService.class);
            conn = new ServiceConnection() {
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    mMessenger = null;
                }
    
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    mMessenger = new Messenger(service);
                }
            };
        }
    
    
    
        @Override
        protected void onStart() {
            super.onStart();
            bindService(intent, conn, BIND_AUTO_CREATE);
        }
    
    
    
        @Override
        protected void onStop() {
            super.onStop();
            unbindService(conn);
        }
    
    
    
        public void onClick(View v){
            Message msg = null;
            switch (v.getId()) {
            case R.id.button1:
                msg = Message.obtain(null, 0, 0, 0);
                try {
                    mMessenger.send(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.Button01:
                msg = Message.obtain(null, 1, 0, 0);
                try {
                    mMessenger.send(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
            }
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

  • 相关阅读:
    [FPGA与ASIC] 优化方法
    学习《中医药治疗热病的作用机理(韩晶岩)》的体会
    使用Capture CIS Lite仿真电路
    Everything软件-集本地文件搜索、文件服务器、批量操作文件等若干功能于一身的高效率软件
    注册表改右键菜单和默认程序
    Qt Quick开发教程4-QML拖放
    Qt Quick开发教程4--使用第三方QML UI包
    个人 PC/Android 常用软件列表
    Qt Quick开发教程3-C++与QML联合开发
    Qt Quick开发教程2-QML语法
  • 原文地址:https://www.cnblogs.com/lenve/p/4770538.html
Copyright © 2011-2022 走看看