Who is calling/starting/stoping my Android app?

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?

Answers


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]
mQueue=android.os.MessageQueue@41ff03a8
  Message 0: { what=100 when=-438ms obj=ActivityRecord{41ff43e0 token=android.os.BinderProxy@41ff3b78 {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.


I need to know for sure - who (what service, why, etc) is calling the application.

Why? You have an elaborate explanation of the scenarios, but no justification.

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?

Not really. The primary exception is if you have a service that is bound to a client, and the client calls a method on your Binder, your Binder can find out the UID of the process that called it. Also, you can find out in some places whether the caller holds a certain permission.

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


Need Your Help

How to get DB object in SailsJS / Waterline adapter methods?

node.js nosql sails.js orientdb waterline

I am trying to write a SailsJS / Waterline adapter for OrientDB using Oriento module for NodeJS.

Restore a Linked Server from backup

sql sql-server backup linked-server

A linked server has been (accidentally) removed from a SQL Server instance and nobody knows the credentials to re-create it from scratch.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.