6

I have an Android app that extends the Application class and has many app components (services, activities and broadcast receivers) running along three different process.

Each process will instantiate the Application class as soon as it is started. So I've been looking for how can I check inside the Application class code, if the instance is owned by the main app process but I have not been able to find it anything.

My manifest looks like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".MyApplication"
    android:theme="@style/AppTheme" >

    <activity
        android:name="...">
        ...
    </activity>

    <activity
        android:name="...">
        ...
    </activity>

    <service android:name="..." >
    </service>

    <service android:name="..."
        android:process=":SecondProcess" >
    </service>

    <service android:name="..."
        android:process=":ThirdProcess" >
    </service>

Next is my Application class implementation:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        if (is this instance being ran on the main process?) {
            // Do something
        }
    }

}

Does anyone know how to check if the instance is running on the main process?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Storo
  • 988
  • 1
  • 7
  • 31
  • 1
    Possible duplicate of [Is there a way to get current process name in Android](http://stackoverflow.com/questions/19631894/is-there-a-way-to-get-current-process-name-in-android) – Nachi Jan 28 '16 at 14:43
  • 1
    @Nachi clearly not a duplicate since the focus of both of them is different. One is a mere approach than can be used in the other. – Storo Jan 28 '16 at 17:01

1 Answers1

0

Looking for the answer for the same question.

So far what i found is use can find the current running process from the process id and put a if check in onCreate.

 public void onCreate(){
 ...
 String processName = getProcessName();
 switch(processName){
    case process1:
         // do something 
         break;
    case process2:
         // do something 
         break;
    ....

 }
user2662821
  • 225
  • 3
  • 12
  • 2
    There is no `getProcessName()` method on the `Application` class. – Storo May 06 '16 at 20:22
  • yes there is no direct method but you can find the process name.. check this http://stackoverflow.com/questions/19631894/is-there-a-way-to-get-current-process-name-in-android – user2662821 May 08 '16 at 19:07