How do I find out the calling activity for a content provider in Android?

As I understand it, in Android the implementation for a content provider cannot and should not be tied to which application is requesting data. As long as the calling application has the required data access permission, the implementation should simply return data based on the request URI. However, I am trying something different in my content provider and need to figure out which application the provider is calling before responding to a data request. Is there a way for my content provider to know about this? Any help would be much appreciated.

0


source to share


1 answer


Assuming you are only interested in whether your application is accessing a provider or a third party application accessing a provider, you can call ContentProvider.getCallingPackage()

and see if the package name matches your application package name.

EDIT: There is one workaround for API level 18 and earlier that I know of. This is a workaround, so it's not perfect, but it works: add an extra piece of data to the URI to identify your application.



So, for example, if the URI for your provider would normally be content://com.example.app/table1

, your application will use the URI content://com.example.app/table1/identifier

. You would add both URI patterns to yours UriMatcher

, using different code for each pattern. Make the URI without an ID public through your contract class, but keep the ID private. Third parties will construct the URI according to the publicly available contracts in your class, and therefore will not include the identifier in the URI. When you create a URI, include the identifier. This way, both URIs will point to the same data, but you can differentiate between your app and third party apps based on what code is returned from match(Uri uri)

. He will not interfere withContentResolver

or because the resolver only checks part of the URI's authority. Again, this assumes that you only need to distinguish between calls from your application.

+2


source







All Articles