【Android】20.使用AIDL跨应用Service传参
程序员文章站
2024-03-23 15:21:10
...
20.使用AIDL跨应用Service传参
1.下载素材。
本节素材源码,请在公众号回复" AS1214 "。
2.在SecondAPP里,修改IMyServiceAidlInterface.aidl。
// IMyServiceAidlInterface.aidl
package jsz.nopi.secondapp;
// Declare any non-default types here with import statements
interface IMyServiceAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
//通过此方法赋值给info变量
void setInfo(String info);
}
3.在SecondAPP里,修改MyService。
public class MyService extends Service {
//是否运行标识符,true -> 运行
private boolean running;
//默认显示的信息
private String info = "正在运行";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IMyServiceAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setInfo(String info) throws RemoteException {
MyService.this.info = info;
}
};
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//获取传过来的数据,如果不为空则赋值。
String info = intent.getStringExtra("info");
if (!TextUtils.isEmpty(info)) {
this.info = info;
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
running = true;
new Thread(new Runnable() {
@Override
public void run() {
while (running) {
String str = info +"......";
System.out.println(str);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
running = false;
}
}
4.在SecondAPP里,修改MainActivity。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(new Intent(this, MyService.class));
}
}
5.在FirstAPP里,新建一个aidl文件夹,新建包名跟SecondAPP的aidl文件夹下的包名一样,把SecondAPP的aidl文件复制到里面。
6.在FirstAPP里,修改MainActivity。
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
//声明变量
private Button btnStart;
private Button btnStop;
private Button btnBind;
private Button btnUnBind;
private Button btnSync;
private EditText edit_info;
private Intent intent;
private IMyServiceAidlInterface binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化控件
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btnStart);
btnStop = findViewById(R.id.btnStop);
btnBind = findViewById(R.id.btnBind);
btnUnBind = findViewById(R.id.btnUnBind);
btnSync = findViewById(R.id.btnSync);
edit_info = findViewById(R.id.edit_info);
//绑定事件
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnUnBind.setOnClickListener(this);
btnSync.setOnClickListener(this);
//实例化服务
intent = new Intent();
//设置为远程服务(SecondAPP提供服务)
intent.setComponent(new ComponentName("jsz.nopi.secondapp", "jsz.nopi.secondapp.MyService"));
}
//处理事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
//启动服务并传参
intent.putExtra("info", edit_info.getText().toString());
startService(intent);
break;
case R.id.btnStop:
//停止服务
stopService(intent);
break;
case R.id.btnBind:
//绑定服务
bindService(intent,this,BIND_AUTO_CREATE);
break;
case R.id.btnUnBind:
//解绑服务
unbindService(this);
break;
//同步服务
case R.id.btnSync:
try {
if (binder != null) {
//把值传到MyService
binder.setInfo(edit_info.getText().toString());
}
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
//绑定服务成功后执行
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//赋值MyService的Binder 给 MainActivity的Binder。
//赋值后可以调用 setInfo()方法传参
binder = IMyServiceAidlInterface.Stub.asInterface(service);
}
//onServiceDisconnected()方法在连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用.
//例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一, 这个时候onServiceDisconnected() 就会被调用。
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
7.效果图。
上一篇: SpringBoot(一) 简介
下一篇: 26.直方图比较