插入链接以在字符串资源中发送Intent

弗朗切斯科Re:

我想在Android应用程序的字符串资源项中添加链接。

我看到可以插入这样的链接

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

但是我不想启动网站,而是想发送一个Intent来将用户带到他的手机设置。

是否可以有这样的链接?

造影

您可以使用DeepLink处理特定URL的。为此,您应该引入Activity作为特定pattern的处理程序URL因此,当用户单击特定的模式链接时,Activity可以将选择为它的处理程序,然后可以打开电话设置。这是此想法的实现:

manifest.xml

<activity android:name=".MyTransientActivity">

    <intent-filter>

        <action android:name="android.intent.action.VIEW" />

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

        <!-- Accepts URIs that begin with "http://somesite.com" -->
        <data android:host="somesite.com" />
        <data android:scheme="http" />

    </intent-filter>

</activity>

MyTransientActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class MyTransientActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent() != null) {
            String action = getIntent().getAction();
            if (action != null && action.equals(Intent.ACTION_VIEW)) {
                Intent settingsIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(settingsIntent);
            }
        }
        finish();
    }

}

测试:

TextView textview = findViewById(R.id.textView);
textview.setText(getString(R.string.my_link));
Linkify.addLinks(textview, Linkify.WEB_URLS);
textview.setMovementMethod(LinkMovementMethod.getInstance());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在查询字符串中发送 JSON

链接以xml字符串资源从android应用发送电子邮件

Rails的活动资源:如何在请求正文中而不是查询字符串中发送post参数?

在字符串中间插入文本(链接)

如何在json字符串中发送隐藏参数?

在RedirectToAction MVC中发送查询字符串

在AJAX数据中发送动态字符串

通过TCP(C ++)在结构中发送字符串

在字符串中发送“&”时遇到问题

如何从非字母或数字的字符串中发送字符到字符串末尾?

使用字符串资源创建超链接

字符串资源

如何在链接列表中动态插入字符串?

在链接列表的开头插入字符串时出错

在链接列表中插入字符串时遇到麻烦

如何插入作为参数发送的字符串?

在改造中发送两个字符串项目作为正文

如何在android中发送字符串作为键盘输出?

如何在android中发送base64字符串?

在ActiveMq 5.15.2中发送和接收文件字符串

在Soap XML信封中发送“ XML数据作为字符串”

为什么我们在大多数当前协议中发送字符串的长度

检索Play框架Java中POST请求中发送的请求正文字符串

在请求正文中而不是在查询字符串中发送参数

如何在Go中的POST请求中发送JSON字符串

Python使用\创建字符串并在json请求中发送

通过电子邮件在查询字符串中发送JWT是否安全?

如何在Swift 4中同时在httpBody中发送字符串值和json值

如何在一台UWP设备中发送跨应用程序字符串?