概念: 在我们的app中调用另一个 app的服务,我们称之为远程服务。需要使用
AIDL
进行 ·IPC·(跨进程通信)。
参考文章: Android:远程服务Service(含AIDL & IPC讲解)
// IRemoteService.aidl
package com.example.www.remoteservice;
// Declare any non-default types here with import statements
interface IRemoteService {
/** Request the process ID of this service, to do evil things with it. */
int getPid();
/** Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
package com.example.www.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AIDLService extends Service {
private String TAG = "REMOTESERVICE";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "DDService onBind");
return mBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(TAG, "DDService onCreate........" + "Thread: " + Thread.currentThread().getName());
}
/*与本地服务不同,此处新建Stub类型的Binder*/
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid() {
Log.d(TAG, "Thread: " + Thread.currentThread().getName());
/*返回给调用者当前的线程编号*/
return (int) Thread.currentThread().getId();
}
public int basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
Log.d(TAG, "Thread: " + Thread.currentThread().getName());
Log.d(TAG, "basicTypes aDouble: " + aDouble + " anInt: " + anInt + " aBoolean " + aBoolean + " aString " + aString);
/*返回给调用者当前的线程编号*/
return (int) Thread.currentThread().getId();
}
};
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.www.remoteservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<service
android:name=".AIDLService"
android:enabled="true"
android:exported="true"
android:process=":xlzh" >
<intent-filter>
<action android:name="com.example.www">action>
intent-filter>
service>
application>
manifest>
package com.example.www.remoteservice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, AIDLService.class));
}
}
至此远程服务创建完毕
// IRemoteService.aidl
package com.example.www.remoteservice;
// Declare any non-default types here with import statements
interface IRemoteService {
/** Request the process ID of this service, to do evil things with it. */
int getPid();
/** Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
package com.example.www.client;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.www.remoteservice.IRemoteService;
public class MainActivity extends AppCompatActivity {
private MyServiceConnection mConn;
private String TAG = "com.example.aidl";
private IRemoteService remoteService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bind(View view) {
/*
* com.example.www 表示 远程服务端 service 配置的action name
* com.example.www.remoteservice 表示 远程服务 Service 所在的 包名
* */
Intent intent = new Intent("com.example.www");
intent.setPackage("com.example.www.remoteservice");
mConn = new MyServiceConnection();
bindService(intent, mConn, Service.BIND_AUTO_CREATE);
}
public void unbind(View view) {
unbindService(mConn);
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "Bind Service success!");
/*获取服务端handle*/
remoteService = IRemoteService.Stub.asInterface(service);
try {
int pid1 = remoteService.getPid();
int pid2 = remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白");
/*打印带有getPid接口和basicTypes接口时服务端的线程号*/
Toast.makeText(getApplicationContext(), pid1 + "---" + pid2, Toast.LENGTH_LONG).show();
Log.d(TAG, "remoteService.getPid(): " + pid1 + " remoteService.basicTypes(): " + pid2);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="bind"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="bind"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="unbind"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
android:onClick="unbind"/>
android.support.constraint.ConstraintLayout>