[英]Android, open url by my application
我有我的自定義網站,我的Android應用程序將獲取數據。
讓我們稱之為example.com
。
我的Android應用程序可以通過WhatsApp等其他應用程序共享鏈接。 共享鏈接時,會創建http://example.com/my/path/to/information
鏈接。
我想在點擊此鏈接時打開我自己的應用程序。 在我的AndroidManifest
文件中,我為我的意圖添加了以下代碼行:
<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="example.com" android:pathPattern="/.*" android:scheme="http"/>
</intent-filter>
我的Activity處理它的意圖如下:
Intent intent = getIntent();
Log.d("My_intent", intent.toString());
mainNewsId = intent.getStringExtra("news_info");
Log.d("News_result", mainNewsId);
當我使用共享鏈接的其他應用程序並單擊它時,它必須向我建議可以打開此鏈接的應用程序。
但是, 它並不建議我的應用程序共享此鏈接 。
我錯過了什么? 我需要在我的網頁上添加meta
標記嗎?
======================== UPDATE ========================= ========
我發現了一個有趣的結果。 在我的意圖過濾器中,如果我在android:host
上寫一些其他熱門網站,它有自己的Android應用程序,它建議在我點擊鏈接時打開我的applcation和它自己的應用程序 。
這是否意味着我需要在我的網頁上寫一些東西?
我通過使用這些方法解決了我的問題。
在我的網頁,我已經添加了link
標簽head
:
<link rel="alternate" href="android-app://com.example.app/example.com/my/path/to/information"/>
在我的清單中,我添加了這個:
<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="example.com" android:pathPattern="/.*" android:scheme="http"/>
<data android:host="example.com" android:pathPattern="/.*" android:scheme="https"/>
<data android:host="example.com" android:pathPattern="/.*" android:scheme="android-app"/>
<data android:scheme="http"/>
</intent-filter>
這很有效。 現在,當我點擊鏈接時,它建議我的應用程序打開它。
嘗試提供特定的路徑前綴。 例如,如果您希望打開http://example.com/my/path/to/information
則意圖過濾器應如下所示
<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="example.com" android:pathPattern="/my/" android:scheme="http"/>
然后,如果您希望使用不同的路徑前綴打開相同的活動,則需要為每個前綴添加數據標記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.