預設瀏覽器手機 - 從android瀏覽器啟動自定義的android應用程序
設定chrome為預設瀏覽器 (10)
任何人都可以請指導我如何從Android瀏覽器啟動我的Android應用程序?
https://code.i-harness.com
Felix處理深層鏈接的方法是處理深層鏈接的典型方法。 我也建議查看這個庫來處理你的深層鏈接的路由和解析:
https://github.com/airbnb/DeepLinkDispatch
您可以使用註釋為特定的深層鏈接URI註冊您的活動,並且它將為您提取出參數,而不必像通常那樣獲取路徑段,匹配它等等。您可以簡單地註釋和活動,像這樣:
@DeepLink("somePath/{someParameter1}/{someParameter2}")
public class MainActivity extends Activity {
...
}
費馬克斯的Xamarin港口的答案
在你的MainActivity
,添加這個( docs: Android.App.IntentFilterAttribute Class ):
....
[IntentFilter(new[] {
Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
....
Xamarin會為您添加以下AndroidManifest.xml
:
<activity android:label="Something" android:screenOrientation="portrait" android:theme="@style/MyTheme" android:name="blahblahblah.MainActivity">
<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:scheme="my.special.scheme" />
</intent-filter>
</activity>
並且為了得到參數( 我在MainActivity的OnCreate中測試過 ):
var data = Intent.Data;
if (data != null)
{
var scheme = data.Scheme;
var host = data.Host;
var args = data.PathSegments;
if (args.Count > 0)
{
var first = args[0];
var second = args[1];
...
}
}
據我所知,以上可以添加到任何活動中,而不僅僅是MainActivity
使用example.php:
<?php
if(!isset($_GET['app_link'])){ ?>
<iframe src="example.php?app_link=YourApp://blabla" style="display:none;" scrolling="no" frameborder="0"></iframe>
<iframe src="example.php?full_link=http://play.google.com/xyz" style="display:none;" scrolling="no" frameborder="0"></iframe>
<?php
}
else { ?>
<script type="text/javascript">
self.window.location = '<?php echo $_GET['app_link'];?>';
window.parent.location.href = '<?php echo $_GET['full_link'];?>';
</script>
<?php
}
例如,你有下面的東西:
打開您的應用的鏈接: http : //example.com
您應用的軟件包名稱:com.example.mypackage
然後你需要做下一個:
1)為你的活動添加一個意圖過濾器(可以是你想要的任何活動,更多信息請查閱documentation )。
<activity
android:name=".MainActivity">
<intent-filter android:label="@string/filter_title_view_app_from_web">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://example.com" -->
<data
android:host="example.com"
android:scheme="http" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2)創建一個HTML文件來測試鏈接或使用此方法。
<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">Open your Activity directly (just open your Activity, without a choosing dialog). </a>
<a href="http://example.com">Open this link with browser or your programm (by choosing dialog).</a>
3)使用移動Chrome進行測試
4)就是這樣。
而且它沒有必要在市場上發布應用程序來測試深層鏈接=)
另外,有關更多信息,請查看documentation和有用的presentation 。
在我的情況下,我必須為<intent-filter>
設置兩個類別,然後它工作:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
您需要將一個偽主機名添加到CALLBACK_URL'app://'作為一個URL沒有意義,並且不能被解析。
是的,Chrome搜索,而不是尋找計劃。 如果您想通過URI方案啟動您的應用程序,請在Play商店中使用此酷實用程序應用程序。 它拯救了我的一天:) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender
請在這裡看到我的評論: 在Android瀏覽器中創建一個鏈接啟動我的應用程序?
我們強烈不鼓勵人們使用他們自己的計劃,除非他們正在界定一個新的全球互聯網計劃。
還應該將<category android:name="android.intent.category.BROWSABLE"/>
添加到intent過濾器,以使活動能夠從鏈接中正確識別。
使用<intent-filter>
和<data>
元素。 例如,要處理到twitter.com的所有鏈接,您需要將它放在您的AndroidManifest.xml
<activity>
中:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然後,當用戶點擊瀏覽器中的twitter鏈接時,系統會詢問他們要完成操作的應用程序:瀏覽器或應用程序。
當然,如果你想提供你的網站和你的應用程序之間的緊密集成,你可以定義你自己的方案:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然後,在您的網絡應用程序中,您可以放置如下鏈接:
<a href="my.special.scheme://other/parameters/here">
當用戶點擊它時,你的應用程序將自動啟動(因為它可能是唯一可以處理my.special.scheme://
類型的uris)。 唯一的缺點是,如果用戶沒有安裝應用程序,他們會得到一個令人討厭的錯誤。 我不確定有什麼方法可以檢查。
編輯:要回答你的問題,你可以使用getIntent().getData()
,它返回一個Uri
對象。 然後可以使用Uri.*
方法來提取所需的數據。 例如,假設用戶點擊了http://twitter.com/status/1234
的鏈接:
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
你可以在Activity
任何地方做上述事情,但你可能會想在onCreate()
做到這一點。 您也可以使用params.size()
來獲取Uri
的路徑段數。 查看javadoc或android開發人員網站,了解可用於提取特定部分的其他Uri
方法。