Last active
June 2, 2016 10:41
-
-
Save wutianlong/ca1e0dea824bc94f7026794a1b7fa515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
不使用 AIDL的情况下,进行跨进程通信IPC跨进程通信,通过messager 传递 | |
以下代码是client向RemoteService发送消息,并返回给client | |
参考地址:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650236008&idx=1&sn=c7a1f1de9ee92e6d7a2d00f04821a8c3&scene=0#wechat_redirect | |
http://www.cnblogs.com/freeliver54/archive/2012/06/13/2547739.html | |
1. Activity ============== | |
public class MainActivity extends AppCompatActivity { | |
Messenger mMessenger; | |
private ServiceConnection mServiceConnection = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { | |
mMessenger = new Messenger(iBinder); | |
mBind = true; | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName componentName) { | |
mBind = false; | |
mMessenger = null; | |
} | |
}; | |
public void onStart(){ | |
super.onStart(); | |
Intent intent = new Intent(); | |
intent.setPackage("com.sohu.tv.testaction"); | |
intent.setClass(this,RemoteService.class); | |
bindService(intent,mServiceConnection,Context.BIND_AUTO_CREATE); | |
} | |
private Messenger mRemoteServiceBack2ClientMessager = new Messenger(new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
if (msg.what == RemoteService.CLIENT_NOTIFI_SERVICE_WHAT) { | |
Toast.makeText(MainActivity.this,"remote service back arg1 " + msg.arg1 + " arg2=" + msg.arg2,Toast.LENGTH_SHORT).show(); | |
} else { | |
super.handleMessage(msg); | |
} | |
} | |
}); | |
private void onButtonClick() { | |
if (mBind) { | |
Message msg = Message.obtain(null, RemoteService.CLIENT_NOTIFI_SERVICE_WHAT, 20, 30); | |
// 为msg设置自己封装的user对象 | |
User user = new User(); | |
user.setAge(13); | |
user.setUsername("111"); | |
Bundle bundle = new Bundle(); | |
bundle.setClassLoader(User.class.getClassLoader()); | |
bundle.putParcelable("user_key", user); | |
msg.setData(bundle); | |
// client自己创建新messager, 并设置到msg上。 RemoteService调用 msg.replyTo.send()来向客户端发送result | |
msg.replyTo = mRemoteServiceBack2ClientMessager; | |
try { | |
mMessenger.send(msg); | |
} catch (Exception e) { | |
Toast.makeText(MainActivity.this, "exception...", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
protected void onStop() { | |
super.onStop(); | |
if (mBind){ | |
unbindService(mServiceConnection); | |
mBind = false; | |
} | |
} | |
} | |
2. RemoteService ========= | |
public class RemoteService extends Service { | |
public static final int CLIENT_NOTIFI_SERVICE_WHAT = 100; | |
final Messenger messager = new Messenger(new IncomingHandler()); | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
Toast.makeText(RemoteService.this, "binding...", Toast.LENGTH_SHORT).show(); | |
return messager.getBinder(); | |
} | |
class IncomingHandler extends Handler { | |
@Override | |
public void handleMessage(Message msg) { | |
if (msg.what == CLIENT_NOTIFI_SERVICE_WHAT) { | |
Bundle bundle = msg.getData(); | |
bundle.setClassLoader(User.class.getClassLoader()); | |
User user = (User) bundle.getParcelable("user_key"); | |
Toast.makeText(RemoteService.this, " service receiver message success" + " obj = " + user.toString() + " arg1 = " + msg.arg1 + " arg2=" + msg.arg2, Toast.LENGTH_SHORT).show(); | |
try { | |
msg.replyTo.send(Message.obtain(null, CLIENT_NOTIFI_SERVICE_WHAT, 12, 13)); | |
}catch(Exception e){ | |
Toast.makeText(RemoteService.this, "remoteService response send msg exception...", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
super.handleMessage(msg); | |
} | |
} | |
3.Manifest.xml ============== | |
<service | |
android:name=".RemoteService" | |
android:process=":remotemyservice"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment