Sunday, September 8, 2013

Android non RPC Inter Process Communication

In normal situations the best way is using the implemented RPC, but in some cases its not possible, for example:
Process A creates Process B.
Process B uses a long lived notification (like an audio app, that lives in the bacground).
Process A need to set process B notification icon, but its not possible via regular RPC, since only after process B is fully initialize, the the PRC can be used, but the notification icon is part of the initialization.

The way to solve it, is to set all the things not possible via RPC, using SharedPrefrences.
The SharedPreferences must be initialized using:
MODE_MULTI_PROCESS
Using this line:
ctx.getSharedPreferences(PrefsName, Context.MODE_MULTI_PROCESS);

Then use the set and get method normally, Process A can set and B can get (and vice versa):


       public static void setRemoteNotificationIconId(Context ctx, int remoteNotificationIconId) {
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.putInt("RemoteNotificationIconId", remoteNotificationIconId);
editor.commit();
}

public static int getRemoteNotificationIconId(Context ctx) {
int remoteNotificationIconId = getSharedPreferences(ctx).getInt("RemoteNotificationIconId", -1);
return remoteNotificationIconId;
}

No comments:

Post a Comment