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?