简体   繁体   English

在SMS上点击链接时启动Android应用程序

[英]Launch android app when a link is tapped on SMS

I don't know if many people have tried this but I am trying a build an app that requires user to tap on a link on the sms he/she receives and this will launch the android app. 我不知道是否有很多人试过这个,但我正在尝试构建一个应用程序,要求用户点击他/她收到的短信链接,这将启动Android应用程序。 Is it possible to do in android? 在android中有可能吗? If yes, how can I do this? 如果是,我该怎么做? I know this can be done in IOS. 我知道这可以在IOS中完成。 Any suggestions or help will be appreciated. 任何建议或帮助将不胜感激。 Thank You. 谢谢。

In you Manifest, under an Activity that you want to handle incoming data from a link clicked in the messaging app, define something like this: 在您的Manifest中,在您希望处理来自在消息传递应用程序中单击的链接的传入数据的活动下,定义如下内容:

<activity android:name=".SomeActivityName" >

    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="com.your_package.something" />
    </intent-filter>

</activity>

The android:scheme="" here is what will ensure that the Activity will react to any data with this in it: 这里的android:scheme=""将确保Activity对其中的任何数据作出反应:

<data android:scheme="com.your_package.something" />

In the SomeActivityName ( Name used as an illustration. Naturally, you will use your own :-) ), you can run a check like this: SomeActivityName名称用作插图。当然,您将使用自己的:-) ),您可以运行如下检查:

Uri data = getIntent().getData();
String strData = data.toString();
if (strScreenName.equals("com.your_package.something://"))  {
    // THIS IS OPTIONAL IN CASE YOU NEED TO VERIFY. THE ACTUAL USAGE IN MY APP IS BELOW THIS BLOCK
}

My app's similar usage: 我的应用程序的用法类似:

Uri data = getIntent().getData();
strScreenName = data.toString()
        .replaceAll("com.some_thing.profile://", "")
        .replaceAll("@", "");

I use this to handle clicks on twitter @username links within my app. 我使用它来处理我的应用程序中的Twitter @username链接的点击。 I need to strip out the com.some_thing.profile:// and the @ to get the username for further processing. 我需要删除com.some_thing.profile://@来获取用户名以进行进一步处理。 The Manifest code, is the exact same (with just the name and scheme changed). Manifest代码完全相同(只更改了namescheme )。

Add an intent-filter to your app that listens for links that follow the format you want your app to be launched on. 向您的应用添加一个intent-filter ,用于侦听符合您希望启动应用的格式的链接。

However, this will be a global listener, and any link that fits the format even outside the SMS app will trigger your app. 但是,这将是一个全球性的倾听者,即使在SMS应用程序之外,任何符合该格式的链接都将触发您的应用程序。 So if the user taps a similar link in the web browser, your app will attempt to respond to it. 因此,如果用户在Web浏览器中点击类似的链接,您的应用将尝试响应它。

Additionally, if there is another app besides yours that can handle this link, Android will create a chooser that allows the user to pick whichever app they want to use. 此外,如果您的其他应用程序可以处理此链接,Android将创建一个选择器,允许用户选择他们想要使用的任何应用程序。 There is nothing you can do about this, except suggest that the user make your app the default handler for such links. 除了建议用户使您的应用程序成为此类链接的默认处理程序之外,您无能为力。

  <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE"
        <data android:scheme="https" android:host="${hostName}" android:pathPattern="/.*" />
  </intent-filter>

Previous answers are OK but don't forget to specify the pattern. 以前的答案都可以,但不要忘记指定模式。

See more details here. 在这里查看更多细节。

Also define your hostname inside Gradle file like: 还要在Gradle文件中定义主机名,如:

android {
    defaultConfig {
        manifestPlaceholders = [hostName:"subdomain.example.com"]
    }
}

More info about manifestPlaceholders here. 有关manifestPlaceholders更多信息,请点击这里。

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

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