1

We're developing an Android SDK that is made to be initialized in the main Application's onCreate() method.

The reason we need to have it initialized in the main Application is that we support Push Notifications that require our SDK to be initialized on tap.

Upon reception of the push the Application's onCreate() fires which ensures this works as intended.

The problem we're having is the same that was reported in this SO article.

The accepted answer in that article suggests starting all services from the Activity Context, which is unfortunately not an option for reasons explained above.

There is however a second answer in that article that suggests the following:

Check process name during the application's on create and not bind if it's not the default process

I am using this code snippet to perform the check:

public static String getCurrentProcessName() throws IOException {
    BufferedReader cmdlineReader = null;

    try {
        cmdlineReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/" + android.os.Process.myPid() + "/cmdline"), "iso-8859-1"));

        int c;

        StringBuilder processName = new StringBuilder();

        while ((c = cmdlineReader.read()) > 0) {
            processName.append((char) c);
        }

        return processName.toString();
    } finally {
        if (cmdlineReader != null) {
            cmdlineReader.close();
        }
    }
}

Can anyone identify a flaw using this method?

If I use this method, will there be any cases where the spawned process name would match the original process name and thus initialize anyway?

Will our SDK's service continue to work correctly if we prevent a new initialization on the extra spawned process?

Community
  • 1
  • 1
  • post your code (and manifesto), if you are binding to a local `Service` you will always get the exact `Binder` that is returned by `onBind` method – pskink Nov 06 '15 at 20:37
  • @pskink if you read this answer in the article I linked you can see that this is not always the case: http://stackoverflow.com/a/30655477/5534839 Explanation in second to last paragraph. – Julian Garritano Nov 06 '15 at 21:07
  • you are binding your services in the wrong place then, your app has two or more processes, each of them having the same custom `Application` class, what you should do is to bind your services from a custom `ContentProvider`, it is created even before `Application` is initialized, see `Application#onCreate` docs: """Called when the application is starting, before any activity, service, or receiver objects (**excluding content providers**) have been created.""" – pskink Nov 07 '15 at 12:54
  • Thanks for the suggestion! We will give that a try. – Julian Garritano Nov 09 '15 at 16:01

0 Answers0