AIDLを使用してIPC通信を実現する--サービス側がクライアントにデータを自発的に送信することを実現する(転送)
21002 ワード
作者:liuyi 1207164339
原文:https://blog.csdn.net/liuyi1207164339/article/details/51708025
前の記事では、クライアントでAIDLを使用してIPC通信を実現し、リモート・サービス・エンドを呼び出す方法について説明しました.しかし、リモート・サービス・エンドは、クライアントに情報を自発的に返すことはできません.多くの場合、リモート・サービス・エンドがクライアントにデータをアクティブに返す必要があり、クライアントは傍受するだけでよいのが典型的なオブザーバ・モードです.この文章は主にこの問題を解決しに来た.
コードは主にApiDemos/App/service/Remote Service Bindingから来ており、コードについて説明します.
1、まずAIDLインタフェース定義
ここでは、3つのインタフェースを定義します.まず、IremoteServiceです.このインタフェースは、主にクライアントの登録と登録コールバックインタフェースに使用され、サービス側がクライアントにデータを返信することができます.
次に、クライアントに情報を返信するコールバックインタフェースであるIremoteServiceCallbackです.AIDLインタフェースでは一般的なインターフェースがサポートされていないため、インタフェースもaidlインタフェースタイプでなければなりません.以下に示します.
最後に別のaidlインタフェースISecondaryで、インタフェースには以下に示す2つの方法が定義されています.サービス側のプロセスのPIDを取得する方法があります.これにより、クライアントがこのPIDに基づいてサービス側のプロセスを殺したときにどのような反応が現れるかを見ることができます.これは後で話します.
2、AIDLインタフェースのサービス側での実現
サービス側のコードは以下の通りです.いくつか説明が必要です.
1.RemoteCallbackListを使用してクライアントからのコールバックインタフェースを保存し、サービス側が受信したオブジェクトとクライアントが同じオブジェクトであることを保証する.
2、IremoteSeviceの2つの方法はそれぞれ登録インタフェースと登録インタフェースであり、RemoteCallback Listのregisterとunregisterメソッドを使用する.
3、ISecondaryのgetPidメソッドでは、現在のサービス側プロセスのPIDを返します.
4、サービスのoncreateメソッドでメッセージをメッセージキューに送信し、Handlerはこのメッセージを受信した後、サービス側に値を送信し、送信が完了した後、1秒おきにメッセージを送信し、クライアントは1秒おきにサービス側から値を受信し、この値は累積された数字である.
5、RemoteCallbackListに保存されているコールバックインタフェースを呼び出す送信データには、以下のように固定された書き方がある.まずブロードキャストを開始し、リストの各項目を取得し、このインタフェースのメソッドを呼び出さなければなりません.登録されたすべてのインタフェースがコールバックされた後、ブロードキャストを終了する必要があります.
リモートサービスコード:
3、マニフェストファイルにサービスを登録する
サービスにはいくつかのactionが追加されており、他のコンポーネントがIntentを介してこのサービスを暗黙的に起動するために使用されます.
4、クライアントの実現
クライアントインタフェースは、主に3つのボタンによってバインドされ、バインドを解除し、サービス側プロセスを殺し、ステータスを表示するテキストコントロールがあります.
レイアウトファイルは次のとおりです.
mHandlerの実装は以下の通りである.
2、UNBIND SERVICEボタンをクリックする場合は、登録前に登録したIremoteServiceCallbackコールバックインタフェースを解除してからunbindServiceを行う必要があります.
3、bindServiceを実行する場合、コードは以下の通りで、3番目のパラメータにはいくつかのオプションがあり、一般的にContext.BIND_を選択します.AUTO_CREATEは、バインド中にサービスプロセスが意外に殺された場合、バインドされたサービスを自動的に再起動することを意味します.したがって、KILL PROCESSボタンをクリックすると、サービスプロセスが殺されますが、すぐに自動的に再起動し、onServiceConnectedメソッドを再バインドします.もちろん、このパラメータには他の選択肢があります.
プライマリActivityコード:
5、実行効果
BIND SERVICEボタンをクリックすると、サービス側からメッセージが1秒おきに受信されます.
KILL PROCESSボタンをクリックすると、まずサービスプロセスが殺され、すぐにサービスプロセスが再起動され、再バインドされます.これはbindServiceの場合、3番目のパラメータがContext.BIND_に設定されているためです.AUTO_CREATEなので、このような効果が現れます.
さらに、
1、IPC通信を行う時、権限を検証することもでき、ある権限を持つAPPだけがこのサービスをバインドしてBinderに戻り、このBinderを使用して通信することができる.権限検証についてはここでは説明しません.
2、同時に返されるIBinderオブジェクトにデスエージェントを設定する必要があります.リモートサービスが何らかの理由で死亡した場合、このコールバックメソッドが呼び出されます.このメソッドでは、再bindServiceなどの操作を行うことができます.これは前節で述べた.
3、以上のルーチンでは、2つのAIDLインタフェースが同じサービスで実装されている場合を示しています.サービスは4つのコンポーネントの1つであり、システムリソースであり、無制限にサービスを追加するのに適していないため、すべてのAIDLを同じサービスの中に置いて管理することが望ましい.すべてのAIDL接続を管理するためのツールを自分で書くことができ、ツールを一例に設定し、クライアントにインタフェースを露出すればいいです.
コードダウンロードアドレス:https://github.com/EasyLiu-Ly/AIDLDemo
原文:https://blog.csdn.net/liuyi1207164339/article/details/51708025
前の記事では、クライアントでAIDLを使用してIPC通信を実現し、リモート・サービス・エンドを呼び出す方法について説明しました.しかし、リモート・サービス・エンドは、クライアントに情報を自発的に返すことはできません.多くの場合、リモート・サービス・エンドがクライアントにデータをアクティブに返す必要があり、クライアントは傍受するだけでよいのが典型的なオブザーバ・モードです.この文章は主にこの問題を解決しに来た.
コードは主にApiDemos/App/service/Remote Service Bindingから来ており、コードについて説明します.
1、まずAIDLインタフェース定義
ここでは、3つのインタフェースを定義します.まず、IremoteServiceです.このインタフェースは、主にクライアントの登録と登録コールバックインタフェースに使用され、サービス側がクライアントにデータを返信することができます.
package com.easyliu.demo.aidl;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
/**
* Example of defining an interface for calling on to a remote service
* (running in another process).
*/
interface IRemoteService {
/**
* Often you want to allow a service to call back to its clients.
* This shows how to do so, by registering a callback interface with
* the service.
*/
void registerCallback(IRemoteServiceCallback cb);
/**
* Remove a previously registered callback interface.
*/
void unregisterCallback(IRemoteServiceCallback cb);
}
次に、クライアントに情報を返信するコールバックインタフェースであるIremoteServiceCallbackです.AIDLインタフェースでは一般的なインターフェースがサポートされていないため、インタフェースもaidlインタフェースタイプでなければなりません.以下に示します.
package com.easyliu.demo.aidl;
/**
* Example of a callback interface used by IRemoteService to send
* synchronous notifications back to its clients. Note that this is a
* one-way interface so the server does not block waiting for the client.
*/
oneway interface IRemoteServiceCallback {
/**
* Called when the service has a new value for you.
*/
void valueChanged(int value);
}
最後に別のaidlインタフェースISecondaryで、インタフェースには以下に示す2つの方法が定義されています.サービス側のプロセスのPIDを取得する方法があります.これにより、クライアントがこのPIDに基づいてサービス側のプロセスを殺したときにどのような反応が現れるかを見ることができます.これは後で話します.
interface ISecondary {
/**
* Request the PID of this service, to do evil things with it.
*/
int getPid();
/**
* This demonstrates the 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);
}
2、AIDLインタフェースのサービス側での実現
サービス側のコードは以下の通りです.いくつか説明が必要です.
1.RemoteCallbackListを使用してクライアントからのコールバックインタフェースを保存し、サービス側が受信したオブジェクトとクライアントが同じオブジェクトであることを保証する.
2、IremoteSeviceの2つの方法はそれぞれ登録インタフェースと登録インタフェースであり、RemoteCallback Listのregisterとunregisterメソッドを使用する.
3、ISecondaryのgetPidメソッドでは、現在のサービス側プロセスのPIDを返します.
4、サービスのoncreateメソッドでメッセージをメッセージキューに送信し、Handlerはこのメッセージを受信した後、サービス側に値を送信し、送信が完了した後、1秒おきにメッセージを送信し、クライアントは1秒おきにサービス側から値を受信し、この値は累積された数字である.
5、RemoteCallbackListに保存されているコールバックインタフェースを呼び出す送信データには、以下のように固定された書き方がある.まずブロードキャストを開始し、リストの各項目を取得し、このインタフェースのメソッドを呼び出さなければなりません.登録されたすべてのインタフェースがコールバックされた後、ブロードキャストを終了する必要があります.
final int N = mCallbacks.beginBroadcast();
for (int i=0; i
リモートサービスコード:
package com.easyliu.demo.aidldemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.easyliu.demo.aidl.IRemoteService;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
import com.easyliu.demo.aidl.ISecondary;
public class RemoteService extends Service {
/**
* This is a list of callbacks that have been registered with the
* service. Note that this is package scoped (instead of private) so
* that it can be accessed more efficiently from inner classes.
*/
final RemoteCallbackList mCallbacks
= new RemoteCallbackList();
private int mValue = 0;
private static final int REPORT_MSG = 1;
@Override
public void onCreate() {
mHandler.sendEmptyMessage(REPORT_MSG);
}
@Override
public void onDestroy() {
// Tell the user we stopped.
Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
// Unregister all callbacks.
mCallbacks.kill();
// Remove the next pending message to increment the counter, stopping
// the increment loop.
mHandler.removeMessages(REPORT_MSG);
}
@Override
public IBinder onBind(Intent intent) {
// Select the interface to return. If your service only implements
// a single interface, you can just return it here without checking
// the Intent.
if (IRemoteService.class.getName().equals(intent.getAction())) {
return mBinder;
}
if (ISecondary.class.getName().equals(intent.getAction())) {
return mSecondaryBinder;
}
return null;
}
/**
* The IRemoteInterface is defined through IDL
*/
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public void registerCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.register(cb);
}
public void unregisterCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.unregister(cb);
}
};
/**
* A secondary interface to the service.
*/
private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
public int getPid() {
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
}
};
@Override
public void onTaskRemoved(Intent rootIntent) {
Toast.makeText(this, "Task removed: " + rootIntent, Toast.LENGTH_LONG).show();
}
/**
* Our Handler used to execute operations on the main thread. This is used
* to schedule increments of our value.
*/
private final Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
// It is time to bump the value!
case REPORT_MSG: {
// Up it goes.
int value = ++mValue;
// Broadcast to all clients the new value.
final int N = mCallbacks.beginBroadcast();
for (int i=0; i
3、マニフェストファイルにサービスを登録する
サービスにはいくつかのactionが追加されており、他のコンポーネントがIntentを介してこのサービスを暗黙的に起動するために使用されます.
4、クライアントの実現
クライアントインタフェースは、主に3つのボタンによってバインドされ、バインドを解除し、サービス側プロセスを殺し、ステータスを表示するテキストコントロールがあります.
レイアウトファイルは次のとおりです.
主Activity代码如下所示。有几点需要说明:
1、点击BIND SERVICE按钮的时候,同时绑定ISecondary和IRemoteService,返回相应的接口。同时,给返回的IRemoteService接口注册一个回调接口,用于接收服务端发来的信息。IRemoteServiceCallback回调接口如下所示,在注释中已经有了说明,由于valuedChanged方法是运行客户端的Binder线程当中,是不能直接访问主UI当中的控件的,所以需要通过Handler切换到主UI线程中去执行。
注意:如果valuedChanged比较耗时的话,必须确保RemoteService当中的valueChanged方法不是运行在主UI当中,不然会导致服务端无法响应。
同理:在客户端调用服务端的方法的时候,如果服务端的方法比较耗时,我们就得避免在客户端的UI线程当中去访问远程方法,不然会导致客户端无响应。
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
/**
* This is called by the remote service regularly to tell us about
* new values. Note that IPC calls are dispatched through a thread
* pool running in each process, so the code executing here will
* NOT be running in our main thread like most other things -- so,
* to update the UI, we need to use a Handler to hop over there.
*/
public void valueChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
mHandlerの実装は以下の通りである.
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
mCallbackText.setText("Received from service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
2、UNBIND SERVICEボタンをクリックする場合は、登録前に登録したIremoteServiceCallbackコールバックインタフェースを解除してからunbindServiceを行う必要があります.
3、bindServiceを実行する場合、コードは以下の通りで、3番目のパラメータにはいくつかのオプションがあり、一般的にContext.BIND_を選択します.AUTO_CREATEは、バインド中にサービスプロセスが意外に殺された場合、バインドされたサービスを自動的に再起動することを意味します.したがって、KILL PROCESSボタンをクリックすると、サービスプロセスが殺されますが、すぐに自動的に再起動し、onServiceConnectedメソッドを再バインドします.もちろん、このパラメータには他の選択肢があります.
bindService(intent,
mConnection, Context.BIND_AUTO_CREATE);
プライマリActivityコード:
package com.easyliu.demo.aidldemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.easyliu.demo.aidl.IRemoteService;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
import com.easyliu.demo.aidl.ISecondary;
public class BindActivity extends AppCompatActivity {
IRemoteService mService = null;
ISecondary mSecondaryService = null;
Button mKillButton;
TextView mCallbackText;
private boolean mIsBound;
private static final int BUMP_MSG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remote_service_binding);
Button button = (Button) findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button) findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
mKillButton = (Button) findViewById(R.id.kill);
mKillButton.setOnClickListener(mKillListener);
mKillButton.setEnabled(false);
mCallbackText = (TextView) findViewById(R.id.callback);
mCallbackText.setText("Not attached.");
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
mCallbackText.setText("Received from service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);
mKillButton.setEnabled(true);
mCallbackText.setText("Attached.");
// We want to monitor the service for as long as we are
// connected to it.
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
// In this case the service has crashed before we could even
// do anything with it; we can count on soon being
// disconnected (and then reconnected if it can be restarted)
// so there is no need to do anything here.
}
// As part of the sample, tell the user what happened.
Toast.makeText(BindActivity.this, R.string.remote_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;
mKillButton.setEnabled(false);
mCallbackText.setText("Disconnected.");
// As part of the sample, tell the user what happened.
Toast.makeText(BindActivity.this, R.string.remote_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
/**
* Class for interacting with the secondary interface of the service.
*/
private ServiceConnection mSecondaryConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// Connecting to a secondary interface is the same as any
// other interface.
mSecondaryService = ISecondary.Stub.asInterface(service);
mKillButton.setEnabled(true);
}
public void onServiceDisconnected(ComponentName className) {
mSecondaryService = null;
mKillButton.setEnabled(false);
}
};
/**
*
*/
private View.OnClickListener mBindListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(IRemoteService.class.getName());
intent.setPackage("com.easyliu.demo.aidldemo");
// Context.BIND_AUTO_CREATE, ,
// Service Destroy ,Android Service。
// Kill Process Service
bindService(intent,
mConnection, Context.BIND_AUTO_CREATE);
intent=new Intent(ISecondary.class.getName());
intent.setPackage("com.easyliu.demo.aidldemo");
bindService(intent,
mSecondaryConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mCallbackText.setText("Binding.");
}
};
/**
*
*/
private View.OnClickListener mUnbindListener = new View.OnClickListener() {
public void onClick(View v) {
if (mIsBound) {
if (mService != null) {
try {
mService.unregisterCallback(mCallback);
} catch (RemoteException e) {
// There is nothing special we need to do if the service
// has crashed.
}
}
// Detach our existing connection.
unbindService(mConnection);
unbindService(mSecondaryConnection);
mKillButton.setEnabled(false);
mIsBound = false;
mCallbackText.setText("Unbinding.");
}
}
};
private View.OnClickListener mKillListener = new View.OnClickListener() {
public void onClick(View v) {
// To kill the process hosting our service, we need to know its
// PID. Conveniently our service has a call that will return
// to us that information.
if (mSecondaryService != null) {
try {
int pid = mSecondaryService.getPid();
// Note that, though this API allows us to request to
// kill any process based on its PID, the kernel will
// still impose standard restrictions on which PIDs you
// are actually able to kill. Typically this means only
// the process running your application and any additional
// processes created by that app as shown here; packages
// sharing a common UID will also be able to kill each
// other's processes.
Process.killProcess(pid);
mCallbackText.setText("Killed service process.");
} catch (RemoteException ex) {
// Recover gracefully from the process hosting the
// server dying.
// Just for purposes of the sample, put up a notification.
Toast.makeText(BindActivity.this,
R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
}
}
};
/**
*
*/
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
/**
* This is called by the remote service regularly to tell us about
* new values. Note that IPC calls are dispatched through a thread
* pool running in each process, so the code executing here will
* NOT be running in our main thread like most other things -- so,
* to update the UI, we need to use a Handler to hop over there.
*/
public void valueChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
}
5、実行効果
BIND SERVICEボタンをクリックすると、サービス側からメッセージが1秒おきに受信されます.
KILL PROCESSボタンをクリックすると、まずサービスプロセスが殺され、すぐにサービスプロセスが再起動され、再バインドされます.これはbindServiceの場合、3番目のパラメータがContext.BIND_に設定されているためです.AUTO_CREATEなので、このような効果が現れます.
さらに、
1、IPC通信を行う時、権限を検証することもでき、ある権限を持つAPPだけがこのサービスをバインドしてBinderに戻り、このBinderを使用して通信することができる.権限検証についてはここでは説明しません.
2、同時に返されるIBinderオブジェクトにデスエージェントを設定する必要があります.リモートサービスが何らかの理由で死亡した場合、このコールバックメソッドが呼び出されます.このメソッドでは、再bindServiceなどの操作を行うことができます.これは前節で述べた.
3、以上のルーチンでは、2つのAIDLインタフェースが同じサービスで実装されている場合を示しています.サービスは4つのコンポーネントの1つであり、システムリソースであり、無制限にサービスを追加するのに適していないため、すべてのAIDLを同じサービスの中に置いて管理することが望ましい.すべてのAIDL接続を管理するためのツールを自分で書くことができ、ツールを一例に設定し、クライアントにインタフェースを露出すればいいです.
コードダウンロードアドレス:https://github.com/EasyLiu-Ly/AIDLDemo