AIDL的理解与使用
跨应用启动服务
前提:已经准备好了一个应用的服务。新建另一个应用程序。
MainActivity.java
package com.example.anotherapplication;import android.content.ComponentName;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Intent serviceIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent();// 通过setComponent来实现对另一个应用程序的服务的抓取 // 参数(“包名”,“文件名”) PS:全路径、显示启动 serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService")); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.start: startService(serviceIntent); break; case R.id.stop: stopService(serviceIntent); break; } }}
xml视图
在模拟器上运行程序,在监控台中可以验证启动了另一个程序的服务。
跨应用绑定服务
基于前面的学习,我们知道绑定一个服务需要使用Binder。但是如何在多个程序之间(不知道程序里面的类)的情况下通信呢。
在之前代码的基础上进行改进:
AnotherApplication.javapackage com.example.anotherapplication;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { private Intent serviceIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent();// 通过setComponent来实现对另一个应用程序的服务的抓取 // 参数(“包名”,“文件名”) PS:全路径、显示启动 serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService")); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.start: startService(serviceIntent); break; case R.id.stop: stopService(serviceIntent); break; case R.id.bind: bindService(serviceIntent,this, Context.BIND_AUTO_CREATE); break; case R.id.unbind: unbindService(this); break; } } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("bind service"); } @Override public void onServiceDisconnected(ComponentName componentName) { }}
App:
在App(应用程序)中新增AIDL,自动生成需要的代码。 startService:package com.example.myapplication;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class startService extends Service { public startService() { } @Override public IBinder onBind(Intent intent) { return new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } }; } @Override public void onCreate() { super.onCreate(); System.out.println("Service started"); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service stoped"); }}