在android中的应用程序的重要地方时他们有相互沟通和整合的能力,可以实现一个程序和另一个程序之间的跨应用通信
示例代码:
app中的Mainactivity代码和绑定的服务代码(APPService)还有AIDL代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// startService(new Intent(MainActivity.this,APPService.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
// stopService(new Intent(MainActivity.this,APPService.class));
}
}
public class APPService extends Service {
public APPService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IserviceBinder.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
APPService.this.data=data;
}
};
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("service onCreate");
new Thread(){
@Override
public void run() { //定义一个线程,在线程中输出data
super.run();
running=true;
while (running){
System.out.println(data);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service onDestory");
running=false;
}
//将变量定义在这里跟定义在最上面是一样的,都在类的作用域内
private String data="默认数据";
private boolean running=false;
}
interface IserviceBinder {
/**
* 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);
void setData(String data);//新增加的接口,在这里添加接口,也必须在service中引入接口
}
anotherapp中的Mainactivity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private Intent serviceintent;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
editText= (EditText) findViewById(R.id.editText);
findViewById(R.id.button5).setOnClickListener(this);
serviceintent=new Intent();
serviceintent.setComponent(new ComponentName("com.jikexueyuan.learnaidl","com.jikexueyuan.learnaidl.APPService"));
//通过setComponent方法跨程序启动service,里面的ComponentName的参数为第一个程序的包名,第二个参数为包名+service类名
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
startService(serviceintent);
break;
case R.id.button2:
stopService(serviceintent);
break;
case R.id.button3:
bindService(serviceintent,this, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(this);
binder=null;
break;
case R.id.button5:
if(binder!=null){
try {
binder.setData(editText.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
System.out.println("666666666666666");
System.out.println(iBinder);
binder=IserviceBinder.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
//private AppServiceRemoteBinder binder=null;
private IserviceBinder binder=null;//
}
跨应用绑定service通信总结:
在app这个程序内写了一个APPService,并且在另一个程序之中绑定这个服务bindService(serviceintent,this, Context.BIND_AUTO_CREATE);并且 在
另一个程序之中写了一个一样的AIDL。
我们所要完成的是同步数据到绑定的服务之中,可以理解成将一个程序中所展现的内容在另外一个程序中也可以显示出来。
观看上面的代码 ,先在another的mainactivity中跨应用绑定服务,然后会自动启动onServiceConnected方法和oncreate方法,并且因为添加了一个线程
所以无限循环。
然后在启动同步数据按钮,启动同步数据按钮,会先判断binder是否为空,在mainactivity的下面定义了一个空的binder(因为这里会调用到AIDL所以用
的是AIDL文件名进行修饰),跟普通的绑定service进行通信一样,在前面的绑定服务中调用了了APPService中的onBind方法,也调用了里面的setData方法
所以binder不为空。所以可以执行同步数据按钮,获取到editext上的内容,并同步到另外一个程序之中。