androidandroid-contentproviderandroid-package-managers

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


Solution

  • 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;
        }