6

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

Passiondroid
  • 1,573
  • 1
  • 16
  • 28

2 Answers2

13

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;
    }
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
8

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.

Iria Somobu
  • 79
  • 1
  • 7