繁体   English   中英

从以特定方式开始和结束的Web链接启动android应用活动

[英]Launch android app activity from web link that both starts and ends in a specific way

有没有一种方法可以启动带有应用程序活动的Web链接,其中该Web链接以特定方式开始和结束?

如果用户单击指向特定网站的Web链接(例如,已在Facebook上或通过电子邮件共享的Web链接)并且以网站地址开头,但仅以“ story.html”结尾,则我想启动一个活动。 因此,例如http://www.storyofmathematics.com/不会打开该应用程序,但http://www.storyofmathematics.com/story.html会打开该应用程序

这可以通过使用适当的意图过滤器将活动添加到清单中来实现

    <activity
        android:name=".WebActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="www.storyofmathematics.com"
                android:scheme="http" />
            <data
                android:host="storyofmathematics.com"
                android:scheme="http" />
        </intent-filter>
    </activity>

然后通过在活动中运行正则表达式

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class WebActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        if (intent.getDataString().matches(
                "http:\\/\\/((www.)?)storyofmathematics.com\\/.*story.html")) {
            // is match - do stuff
        } else {
            // is not match - do other stuff
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM