猿问

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

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

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

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

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

可以有这样的链接吗?


千巷猫影
浏览 188回答 2
2回答

慕勒3428872

您可以使用DeepLinks来处理特定URL的 s。为此,您应该引入一个Activity作为特定模式的处理程序URL。因此,当用户单击特定模式链接时,Activity可以选择您作为其处理程序,然后您可以打开电话设置。这是这个想法的一个实现:清单.xml<activity android:name=".MyTransientActivity">&nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.VIEW" />&nbsp; &nbsp; &nbsp; &nbsp; <category android:name="android.intent.category.DEFAULT" />&nbsp; &nbsp; &nbsp; &nbsp; <category android:name="android.intent.category.BROWSABLE" />&nbsp; &nbsp; &nbsp; &nbsp; <!-- Accepts URIs that begin with "http://somesite.com" -->&nbsp; &nbsp; &nbsp; &nbsp; <data android:host="somesite.com" />&nbsp; &nbsp; &nbsp; &nbsp; <data android:scheme="http" />&nbsp; &nbsp; </intent-filter></activity>MyTransientActivity.javaimport android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;public class MyTransientActivity extends AppCompatActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; if (getIntent() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String action = getIntent().getAction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (action != null && action.equals(Intent.ACTION_VIEW)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent settingsIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getApplicationContext().startActivity(settingsIntent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; }}测试:TextView textview = findViewById(R.id.textView);textview.setText(getString(R.string.my_link));Linkify.addLinks(textview, Linkify.WEB_URLS);textview.setMovementMethod(LinkMovementMethod.getInstance());

aluckdog

因为我知道你不能这样发送,你应该像这样添加它:<string name="my_link">http://somesite.com/</string>要发送意图,您必须像这样发送它:String url = this.getResources().getString(R.string.my_link);&nbsp; &nbsp; &nbsp; &nbsp; Intent i = new Intent(Intent.ACTION_VIEW);&nbsp; &nbsp; &nbsp; &nbsp; i.setData(Uri.parse(url));&nbsp; &nbsp; &nbsp; &nbsp; startActivity(i);这将使用您发送的链接打开您的安卓浏览器。我希望这很有用。:)
随时随地看视频慕课网APP

相关分类

Java
我要回答