小編給大家分享一下Android使用短信鏈接打開(kāi)APP的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
短信鏈接跳轉(zhuǎn)APP
平時(shí)我們會(huì)收到廣告短信,比如某東,某寶,里面附加著鏈接,當(dāng)你點(diǎn)開(kāi)鏈接(手機(jī)自帶的瀏覽器),發(fā)現(xiàn)瀏覽器打開(kāi)后,等一下下,就會(huì)打開(kāi)對(duì)應(yīng)的APP,直接到廣告相應(yīng)的頁(yè)面。
Android端的代碼
從簡(jiǎn)單的開(kāi)始,第一個(gè)啟動(dòng)的Activity先來(lái)處理
<activity android:name=".activity.ActivityFirst"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 不可以把上面,桌面啟動(dòng)圖標(biāo)的intent-filter,跟下面短信打開(kāi)App的intent-filter寫一起,否者沒(méi)有桌面圖標(biāo)--> <!-- 在啟動(dòng)的activity加入以下代碼,其中scheme很重要,短信啟動(dòng)App的標(biāo)識(shí)吧 --> <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代碼,關(guān)鍵就是href,就是之前Android啟動(dòng)intent-filter的 “ [scheme的內(nèi)容]” + “ :” 這個(gè)冒號(hào)不能少
<!DOCTYPE html> <html> <head> <title>Android短信測(cè)試</title> </head> <body> <a href="baozi:" rel="external nofollow" >啟動(dòng)程序</a> </body> </html>
3.測(cè)試一下,能不能啟動(dòng)App,我們沒(méi)有服務(wù)器的情況下,可以把這段HTML代碼拷貝到手機(jī)里,點(diǎn)擊選擇品牌自帶瀏覽器啟動(dòng)就可以啦。
基本啟動(dòng)功能.gif
最基本的功能實(shí)現(xiàn)啦,然后我再傳遞參數(shù),打開(kāi)指定的頁(yè)面。
1.HTML的跳轉(zhuǎn)鏈接里面添加參數(shù)
<a href=" scheme的內(nèi)容 :// host的內(nèi)容?傳遞參數(shù)的key=傳遞參數(shù)的value" rel="external nofollow" >隨意什么內(nèi)容...</a> <a href="baozi://bao.cn?type=red&url=111&name=紅色" rel="external nofollow" rel="external nofollow" >啟動(dòng)紅色程序</a> <a href="baozi://bao.cn?type=yellow&name=黃色" rel="external nofollow" rel="external nofollow" >啟動(dòng)黃色色程序,url空</a> <a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow" >啟動(dòng)綠色程序,name空</a>
scheme:?jiǎn)?dòng)的App的標(biāo)識(shí),相當(dāng)于協(xié)議吧。
host:域名,不重要。
query:傳給app參數(shù)的Key和Value 。
2.Android代碼,在第一啟動(dòng)頁(yè)加入下面代碼
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); //標(biāo)題轉(zhuǎn)UTF-8碼 if (!TextUtils.isEmpty(name)) { try { name = URLDecoder.decode(name, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
參數(shù)可以傳空的,如果是中文要轉(zhuǎn)碼,斷點(diǎn)看看參數(shù)
3.測(cè)試。
參數(shù)跳轉(zhuǎn).gif
4.總結(jié),短信跳轉(zhuǎn)App難度不大,就是基本用原生或者chrome內(nèi)核的瀏覽器,支持跳轉(zhuǎn),其他瀏覽器兼容問(wèn)題會(huì)有。
5.代碼不多,就直接放出來(lái)。
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); //如果是從網(wǎng)址打開(kāi)的 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); //標(biāo)題轉(zhuǎn)UTF-8碼 if (!TextUtils.isEmpty(name)) { try { name = URLDecoder.decode(name, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //獲取到的參數(shù)跳轉(zhuǎn) 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短信測(cè)試</title> </head> <body> <a href="baozi://bao.cn?type=red&url=111&name=紅色" rel="external nofollow" rel="external nofollow" >啟動(dòng)紅色程序</a> <br> <a href="baozi://bao.cn?type=yellow&name=黃色" rel="external nofollow" rel="external nofollow" >啟動(dòng)黃色色程序,url空</a> <br> <a href="baozi://bao.cn?type=green&url=333" rel="external nofollow" rel="external nofollow" >啟動(dòng)綠色程序,name空</a> </body> </html>
Manifest代碼最上面有了
以上是“Android使用短信鏈接打開(kāi)APP的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:Android使用短信鏈接打開(kāi)APP的方法-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://jinyejixie.com/article48/dphcep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、營(yíng)銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航、軟件開(kāi)發(fā)、定制網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容