pratsJ
pratsJ

Reputation: 3443

Android getting package name of application from received share intent

In my android application, I have registered for intent filter android.intent.action.SEND in one of my activities to receive data from other applications.

        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
            <data android:mimeType="application/pdf" />
            <data android:mimeType="text/x-vcard" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
            <data android:mimeType="application/pdf" />
        </intent-filter>

When data is shared from other apps with my app using share intent, this activity receives the intent. I would like to know the package name of app which shared the data with my app.

I tried following code but it always returns null.

final String packageName = this.getCallingPackage()

Is there any way I can get the package name of app that shared data with my app using intent that i am receiving?

Upvotes: 3

Views: 3184

Answers (2)

CoderSpinoza
CoderSpinoza

Reputation: 2165

To use getCallingPackage(), the caller should use startActivityForResult(Intent, int) instead of startActivity(). Otherwise, it will return null. Straight from the doc:

Note: if the calling activity is not expecting a result (that is it did not use the startActivityForResult(Intent, int) form that includes a request code), then the calling package will be null.

https://developer.android.com/reference/android/app/Activity.html#getCallingPackage%28%29

Upvotes: 1

cketti
cketti

Reputation: 1377

If the share intent was created using ShareCompat.IntentBuilder it will contain the extra EXTRA_CALLING_PACKAGE.
But most apps don't provide this information. And if the extra is present you should not use it for any security-critical functionality because senders are free to provide any value they like.

Upvotes: 2

Related Questions