使用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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)