计划主机不在Android棒棒糖上工作,点击链接打开应用程序

Sal*_*aan 16 android hyperlink intentfilter android-intent android-5.0-lollipop

我正在使用这段代码从链接启动我的应用程序.

<activity
        android:name="com.example.myApp.myClass"
        android:label="@string/app_name" >
    <intent-filter>
        <data
            android:host="customHostName"
            android:scheme="customScheme" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>  
Run Code Online (Sandbox Code Playgroud)

这是href链接,我想最终获得密钥.

customScheme://customHost/49FYTJTF00
Run Code Online (Sandbox Code Playgroud)

它在以前的所有Android版本上运行良好,但不适用于Lollipop.
当我点击链接时,它只显示要启动的浏览器列表.

我该怎么办?

Rol*_*f ツ 21

编辑:

经过测试和测试,我发现如果您的方案包含大写字符,浏览器将无法启动它.您的方案应该只包含小写字符!

另请注意,chrome项目的bug 459156仍然不允许您直接启动url,您应该将用户引用到包含此JavaScript代码的网页:

<!DOCTYPE html>
<html>
    <body>
        <script language="javascript">
            window.location = 'customscheme://customHost/49FYTJTF00';
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

登录此页面的用户将自动通过"活动选择器"对话框提示或直接发送到您的"活动".

要试用它,请打开Android浏览器,转到下面的网址,然后将以上代码段复制粘贴到编辑器中:http://www.w3schools.com/html/tryit.asp? filename = tryhtml_intro

Gif显示浏览器+ JavaScript打开Activity

GIF

原帖

我试用了你的自定义URI,它适用于Android 5.0

但是你应该知道以下两个错误/问题:

  1. Chromium项目的错误459156这基本上意味着从Android浏览器启动自定义方案不起作用,除非使用JavaScript应用URL.有关解决方法,请参阅此StackOverflow帖子:OAuth和自定义方案导致Chrome中的"ERR_UNKNOWN_URL_SCHEME"
  2. 错误80971:自定义方案的URI在SMS文本中无法点击(Linkify?)

我的方法

我创建了两个独立的活动,一个作为意图接收器,另一个作为意图启动器.启动活动有一个EditText,可以输入完整的URI,还有一个按钮来启动输入的URI.

Main.java onClick(启动活动)

@Override
public void onClick(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(input.getText().toString()));
    try {
        startActivity(intent);
    } catch(ActivityNotFoundException e){
        Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

manifest.xml(仅限活动)

注意<data android:pathPattern=".*"/>部分.这一部分非常重要,因此主持人之后的所有内容都将被视为有效.

<activity
    android:name=".Main"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name=".UriActivity"
    android:label="@string/app_name">

    <!--
    This intent-filter will open any URI with the following forms:

    customscheme://customHost/49FYTJTF00
    customscheme://customHost
    https://www.google.com/something/something
    http://www.google.com/
    http://google.com/something
    -->

    <intent-filter>
        <data android:scheme="https"/>
        <data android:scheme="http"/>
        <data android:host="google.com"/>
        <data android:host="www.google.com"/>

        <data android:scheme="customscheme"/>
        <data android:host="customHost"/>

        <data android:pathPattern=".*"/>

        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

UriActivity.java(接收活动)

public class UriActivity extends ActionBarActivity {

    private TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uri);
        tvText = (TextView) findViewById(R.id.text);
        setTextFromIntent();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setTextFromIntent();
    }

    private void setTextFromIntent(){
        StringBuilder text = new StringBuilder();

        Uri data = getIntent().getData();
        if(data != null){
            text.append("Path:\n");
            text.append(data.getPath());

            text.append("\n\nScheme:\n");
            text.append(data.getScheme());

            text.append("\n\nHost:\n");
            text.append(data.getHost());

            text.append("\n\nPath segments:\n");
            text.append(Arrays.toString(data.getPathSegments().toArray()));
        } else {
            text.append("Uri is null");
        }
        tvText.setText(text);
    }
}
Run Code Online (Sandbox Code Playgroud)

截图:

将自定义意图发送到UriActivity的结果

测试项目下载:

如果你不想自己尝试一下,我下载了该项目.


Ahm*_*lan 6

请使用pathprefix.

   android:pathPrefix="/"
Run Code Online (Sandbox Code Playgroud)

它可能会解决您的问题.请关注android开发者指南网址.