how to get URL from ACTION_SEND?

2019-05-29 23:00发布

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?

1条回答
放荡不羁爱自由
2楼-- · 2019-05-29 23:23

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 
} 
查看更多
登录 后发表回答