Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get application package name or UID which is trying to bind my service from onBind function?

Tags:

android

I have a service in an application, and I can reach this service from different applications. And when applications are tried to bind this service I want to know which application is trying to bind my service in onBind function, but I can't get the package name or UID of this application in onBind function.

Is it possible to get the application name or UID which is trying to bind my service in onBind function?

like image 483
umituzun84 Avatar asked Oct 16 '12 15:10

umituzun84


People also ask

How do I find my Android Package ID?

One method to look up an app's package name is to find the app in the Google Play app store using a web browser. The package name will be listed at the end of the URL after the '? id='.

What is App UID?

Android assigns a unique user ID (UID) to each Android application and runs it in its own process. Android uses this UID to set up a kernel-level Application Sandbox.


3 Answers

You can use the following to determine the calling application.

 String callingApp = context.getPackageManager().getNameForUid(Binder.getCallingUid());

It's important to note the JavaDoc for getCallingUid() which says:

Return the Linux uid assigned to the process that sent you the current transaction that is being processed. This uid can be used with higher-level system services to determine its identity and check permissions. If the current thread is not currently executing an incoming transaction, then its own uid is returned.

like image 199
Durairaj Packirisamy Avatar answered Oct 16 '22 08:10

Durairaj Packirisamy


You can't do this.

onBind() is called from the Android "lifecycle manager" (making up a helpful name), and will only be called once for each Intent (so it can learn which Binder should be returned for that Intent).

The calls to your service then come in over that Binder, and you can do Binder.getCallingUid() in any one of those methods.

like image 44
iRant Avatar answered Oct 16 '22 08:10

iRant


The accepted answer was not quite right! Why? If two or more applications use the same android:sharedUserId, the method Binder.getCallingUid() will return a same uid and getPackageManager().getNameForUid(uid) will return a same string, it looks like: com.codezjx.demo:10058, but is not a package name!

The right way is use the pid:

int pid = Binder.getCallingPid();

And then use pid to get package name by ActivityManager, each process can hold multiple packages, so it looks like:

private String[] getPackageNames(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
    if (infos != null && infos.size() > 0) {
        for(RunningAppProcessInfo info : infos) {
            if(info.pid == pid) {
                return info.pkgList;
            }
        }
    }
    return null;
}

Warnning: When using method Binder.getCallingPid() and if the current thread is not currently executing an incoming transaction, then its own pid is returned. That means you need to call this method in AIDL exposed interface method.

like image 5
codezjx Avatar answered Oct 16 '22 08:10

codezjx