[英]Android open url in my browser
通過單擊鏈接可打開默認瀏覽器,是否可以使單擊時打開我的應用程序-瀏覽器(如果已安裝,如果未安裝,則打開默認瀏覽器)? 我的代碼:
String url = intent.getStringExtra("url");
if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
context.startActivity(i);
解決方案是在清單中定義方案,例如:
<activity
android:name=".MyMainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="myapp" /> <!-- see this -->
</intent-filter>
</activity>
在Activity
添加以下內容
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Handle intent extras
}
網絡瀏覽器中的鏈接類似於: myapp:open_url = http://www.google.fi
然后,您可以從Intent
Extras中解析該網址,然后在WebView
中將其打開
這也是一個有趣的帖子: http : //danielmuller.asia/2013/02/android-open-an-app-from-web-link-or-fallback-to-market/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.