【问题标题】:how to get URL from ACTION_SEND?如何从 ACTION_SEND 获取 URL?
【发布时间】:2012-03-12 23:44:42
【问题描述】:

我的应用正在注册接收网址的意图因此当用户分享网址时,我的应用将出现在应用列表中。

<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>

-

在我的 MainActivity 的 onCreate() 方法中:

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
}

-

我遇到的问题是:数据为空,这很奇怪。由于此代码与ACTION_VIEW 完美配合。但是,它不适用于ACTION_SEND

如何修改它才能工作?

【问题讨论】:

    标签: android android-intent intentfilter


    【解决方案1】:

    你可以试试

    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 
    } 
    

    【讨论】:

    • 完美!还有一个问题:为什么我需要检查Intent.hasExtra()
    • @iturki 我只是认为最好在检索数据之前检查数据是否存在。避免烦人的 NullPointerException。
    • 每次用户在浏览器中导航到一个新的 URL 时都会触发这个吗?
    猜你喜欢
    • 2023-03-10
    • 2017-03-26
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多