• 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;
            }
        }
    }
  • 相关阅读:
    一些文件的扩展名
    关于git,从svn转到git
    trousers--------dpkg: 处理软件包 trousers (--configure)时报错
    Ubuntu下运行DrClient以上网
    Ubuntu下的终端命令--复制文件从一个文件夹到另一个文件夹下
    VSCode放大字体的快捷键
    opessl版本过低造成的函数使用错误
    python的基本语法
    ubuntu和windows的解码方式
    ubuntu下强制删除文件夹
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461776.html
Copyright © 2020-2023  润新知