문제

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. Is it possible to do in android? If yes, how can I do this? I know this can be done in 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:

<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:

<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:

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. I need to strip out the com.some_thing.profile:// and the @ to get the username for further processing. The Manifest code, is the exact same (with just the name and scheme changed).

다른 팁

Add an intent-filter to your app that listens for links that follow the format you want your app to be launched on.

However, this will be a global listener, and any link that fits the format even outside the SMS app will trigger your app. So if the user taps a similar link in the web browser, your app will attempt to respond to it.

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. 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:

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

More info about manifestPlaceholders here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top