Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the package name of the app which access content provider of my app?

Actually i want some kind of Broadcast when any other app fetches the data from the content provider shared by my app

like image 422
Passiondroid Avatar asked Oct 01 '15 10:10

Passiondroid


2 Answers

you can use Binder.getCallingUid() to get uid of calling application. then use getPackageManager().getNameForUid(uid) to get package name of calling app.

Example:

@Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = sUriMatcher.match(uri);
        String callingPackageName = getContext().getPackageManager().getNameForUid(
                Binder.getCallingUid());
        Log.d(TAG, "calling Package Name::" + callingPackageName);

        if (callingPackageName.equals(PKG_MY_PACKAGE)) {
           //do what you want
        }
        .
        .
        .
        return uri;
    }
like image 73
Rahul Tiwari Avatar answered Nov 09 '22 11:11

Rahul Tiwari


Maybe I'm pretty late, but in API19 and above you can just call

getCallingPackage();

inside your ContentProvider to do this trick.

Look at Android Reference for more details.

like image 25
iillyyaa2033 Avatar answered Nov 09 '22 13:11

iillyyaa2033