Server之间传值的奥秘
第一种 Messenger 信使传值
server 传值
package com.example.bpp_server.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.util.Log;
public class MyService extends Service {
public MyService() {
}
Messenger messenger = new Messenger(new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
String str = bundle.getString("str");
Log.i("---", "客户端的:"+str);
//发送至客户端
Messenger replyTo = msg.replyTo;
Message obtain = Message.obtain();
Bundle bundle1 = new Bundle();
bundle1.putString("back","我也爱你,客户端");
obtain.setData(bundle1);
try {
replyTo.send(obtain);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
}
client 中
package com.example.bpp_client;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("bpp.server");
intent.setPackage("com.example.bpp_server");
final Messenger clientMessenger = new Messenger(new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
String back = data.getString("back");
Log.i("---", "handleMessage: 服务器的:"+back);
}
});
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Messenger messenger = new Messenger(service);
Message obtain = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("str","我爱你服务器");
obtain.setData(bundle);
obtain.replyTo = clientMessenger;
try {
messenger.send(obtain);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
}
第二种 AIDL 传值
首先在main Activity中创建一个自定义名(一般叫aidl)的包,其中新建IMyAIDLInterface
// IMyAidlInterface.aidl
package com.example.app_aidl_a;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
int add(int a,int b);
void callShow();
}
之后是在server中
package com.example.app_aidl_a;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
IBinder iBinder = new IMyAidlInterface.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a+b;
}
@Override
public void callShow() throws RemoteException {
show();
}
};
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
public void show(){
Log.i("---", "show: "+"我被调用了");
}
}
之后再client客户端中
要创建一个与服务器同包同名的 aidl
// IMyAidlInterface.aidl
package com.example.app_aidl_a;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
int add(int a,int b);
void callShow();
}
只后在client的MainActivity中
package com.example.app_aidl_b;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.app_aidl_a.IMyAidlInterface;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setPackage("com.example.app_aidl_a");
intent.setAction("pan.test");
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
int add = iMyAidlInterface.add(5, 3);
iMyAidlInterface.callShow();
Toast.makeText(MainActivity.this, "结果是:->"+add, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
}
这样就差不多了
本文深入探讨了Android应用中Server间传值的两种主要方法:Messenger信使传值与AIDL传值。通过实例详细解析了Messenger如何实现server与client之间的消息传递,以及AIDL如何在不同进程间进行数据交换,为开发者提供了实践指南。

1143

被折叠的 条评论
为什么被折叠?



