0 votes
in Education by (1.7m points)
Is there a way to know who is the application caller?

These are several use cases cases:

I have an application and a widget of it. Application is not running. Once I add/remove the widget, the system calls the application to run (Application.onCreate() is called and etc..). So I need to know that this is not a user who press an icon to run application, but this is a system needs to update widget.

When I get low of memory, the system kills the application and then restors it again when there is enough memory. The question is the same as above.

More similar cases...

The only case that I can manage is a BootUpReceiver which is extended from BroadcastReceiver. Only in this case I am sure that the application is run by the "launch at start" feature.

I need to know for sure - who (what service, receiver, intent, etc) any why is calling my application.

Is there a way to know the caller source, or caller ID or any information about a service or any other who call my application to run?

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
As @CommonsWare already mentioned:

the better answer is to redesign your application such that you do not care why your code is being run.

But actually you can get a general idea about why your application was called by dumping message query of MainLooper. You can use following code to do this:

public static void dumpMainLooper(Context context) {

    Looper looper = context.getMainLooper();

    try {

        looper.dump(new Printer() {

            @Override

            public void println(String s) {

                Log.d(LOG_TAG, "- " + s);

            }

        }, "AppMainLooperDump");

    } catch (Throwable th) {

        Log.d(LOG_TAG, "App Main Looper error", th);

    }

}

You shouldn't use this code for any "logic" in your app, but you can use it for debugging and/or logging to get a general idea about Intents sent by system to your app to start it. You should call this method as early as possible, and in logs you will see something like this:

Looper{41ff0388}

mRun=true

mThread=Thread[main,5,main]

[email protected]

  Message 0: { what=100 when=-438ms obj=ActivityRecord{41ff43e0 [email protected] {com.your.package/com.your.package.MainActivity}} }

  Message 1: { what=132 when=+9s911ms }

(Total messages: 2)

Message N - this is a messages which was send by the system to your app. Here you will see defferent messages and this messages should give you a general idea about callers of your app.
...