Android Studio 1-16 Service进程间通信
方法一
服务端
package com.example.messager_server;
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.os.RemoteException;
import android.widget.Toast;
import androidx.annotation.NonNull;
public class MyService extends Service {
final Messenger messenger = new Messenger(new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Bundle data = msg.getData();
String string = data.getString("hello", "king");
Toast.makeText(MyService.this, ""+string, Toast.LENGTH_SHORT).show();
Messenger replyTo = msg.replyTo;
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("person","诸葛亮");
message.setData(bundle);
try {
replyTo.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
public MyService() {
}