Search code examples
androidandroid-intentintentfilter

how to get URL from ACTION_SEND?


My app is registering an intent to receive URLs so when the user shares a url, my app will be in the list of apps.

<intent-filter
    android:label="my app">
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

-

in my MainActivity's onCreate() method:

String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_SEND)) {
    Uri data = intent.getData();
    String s = data.toString();
    output.setText(s); //output: a TextView that holds the URL
}

-

The problem I got is: data is null which is strange. Since this code works perfectly with ACTION_VIEW. However, it didn't work with ACTION_SEND.

How could I modify it to work?


Solution

  • You can try

    String action = intent.getAction(); 
    if (action.equalsIgnoreCase(Intent.ACTION_SEND) && intent.hasExtra(Intent.EXTRA_TEXT)) { 
        String s = intent.getStringExtra(Intent.EXTRA_TEXT); 
        output.setText(s); //output: a TextView that holds the URL 
    }