溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android使用短信鏈接打開APP的方法

發(fā)布時間:2021-02-18 14:25:07 來源:億速云 閱讀:440 作者:小新 欄目:移動開發(fā)

小編給大家分享一下Android使用短信鏈接打開APP的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

短信鏈接跳轉APP

平時我們會收到廣告短信,比如某東,某寶,里面附加著鏈接,當你點開鏈接(手機自帶的瀏覽器),發(fā)現(xiàn)瀏覽器打開后,等一下下,就會打開對應的APP,直接到廣告相應的頁面。

Android端的代碼

從簡單的開始,第一個啟動的Activity先來處理

<activity android:name=".activity.ActivityFirst">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <!-- 不可以把上面,桌面啟動圖標的intent-filter,跟下面短信打開App的intent-filter寫一起,否者沒有桌面圖標-->
  <!-- 在啟動的activity加入以下代碼,其中scheme很重要,短信啟動App的標識吧 -->
  <intent-filter>
    <data android:scheme="baozi" />
 
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
  </intent-filter>
</activity>

2.HTML代碼,關鍵就是href,就是之前Android啟動intent-filter的 “ [scheme的內容]” + “ :” 這個冒號不能少

<!DOCTYPE html>
<html>
<head>
  <title>Android短信測試</title>
</head>
<body>
  <a href="baozi:" rel="external nofollow" >啟動程序</a>
</body>
</html>

3.測試一下,能不能啟動App,我們沒有服務器的情況下,可以把這段HTML代碼拷貝到手機里,點擊選擇品牌自帶瀏覽器啟動就可以啦。

Android使用短信鏈接打開APP的方法

基本啟動功能.gif

最基本的功能實現(xiàn)啦,然后我再傳遞參數(shù),打開指定的頁面。

1.HTML的跳轉鏈接里面添加參數(shù)

<a href=" scheme的內容 :// host的內容?傳遞參數(shù)的key=傳遞參數(shù)的value" rel="external nofollow" >隨意什么內容...</a>

<a href="baozi://bao.cn?type=red&url=111&name=紅色" rel="external nofollow" rel="external nofollow" >啟動紅色程序</a>
<a href="baozi://bao.cn?type=yellow&name=黃色" rel="external nofollow" rel="external nofollow" >啟動黃色色程序,url空</a>
<a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow" >啟動綠色程序,name空</a>

scheme:啟動的App的標識,相當于協(xié)議吧。

host:域名,不重要。

query:傳給app參數(shù)的Key和Value 。

2.Android代碼,在第一啟動頁加入下面代碼

public static final String TYPE_INTENT = "type";
  public static final String URL_INTENT = "url";
  public static final String NAME_INTENT = "name";

  if (intent.getData() != null)
    {
      Uri uri = intent.getData();
      uri.getScheme();//獲取scheme
      uri.getHost();//獲取host
      uri.getAuthority();//獲取authority
      String type = uri.getQueryParameter(TYPE_INTENT);//獲取傳遞參數(shù)
      String url = uri.getQueryParameter(URL_INTENT);
      String name = uri.getQueryParameter(NAME_INTENT);
      //標題轉UTF-8碼
      if (!TextUtils.isEmpty(name))
      {
        try
        {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e)
        {
          e.printStackTrace();
        }
      }
    }

參數(shù)可以傳空的,如果是中文要轉碼,斷點看看參數(shù)

Android使用短信鏈接打開APP的方法

3.測試。

Android使用短信鏈接打開APP的方法

參數(shù)跳轉.gif

4.總結,短信跳轉App難度不大,就是基本用原生或者chrome內核的瀏覽器,支持跳轉,其他瀏覽器兼容問題會有。

5.代碼不多,就直接放出來。

ActivityFirst代碼

public class ActivityFirst extends AppCompatActivity
{
  public static final String TYPE_INTENT = "type";
  public static final String URL_INTENT = "url";
  public static final String NAME_INTENT = "name";
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_activity);

    //如果是從網址打開的
    Intent intent = getIntent();
    if (intent.getData() != null)
    {
      Uri uri = intent.getData();
      uri.getScheme();//獲取scheme
      uri.getHost();//獲取host
      uri.getAuthority();//獲取authority
      String type = uri.getQueryParameter(TYPE_INTENT);
      String url = uri.getQueryParameter(URL_INTENT);
      String name = uri.getQueryParameter(NAME_INTENT);
      //標題轉UTF-8碼
      if (!TextUtils.isEmpty(name))
      {
        try
        {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e)
        {
          e.printStackTrace();
        }
      }

      //獲取到的參數(shù)跳轉
      Intent intentStart = new Intent(this,ActivityMain.class);
      intentStart.putExtra(TYPE_INTENT,type);
      intentStart.putExtra(URL_INTENT,url);
      intentStart.putExtra(NAME_INTENT,name);
      startActivity(intentStart);
      finish();
    }
  }
}

HTML代碼

<!DOCTYPE html>
<html>
<head>
  <title>Android短信測試</title>
</head>
<body>
  <a href="baozi://bao.cn?type=red&url=111&name=紅色" rel="external nofollow" rel="external nofollow" >啟動紅色程序</a>
  <br>
  <a href="baozi://bao.cn?type=yellow&name=黃色" rel="external nofollow" rel="external nofollow" >啟動黃色色程序,url空</a>
  <br>
  <a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow" >啟動綠色程序,name空</a>

</body>
</html>

Manifest代碼最上面有了

以上是“Android使用短信鏈接打開APP的方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI