2014-09-02 99 views
0

通過點擊鏈接打開一個默認瀏覽器,是否有可能讓點擊打開我的應用程序 - 瀏覽器,如果安裝了,如果沒有則打開默認瀏覽器? 我的代碼:Android在我的瀏覽器中打開網址

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); 

回答

0

解決方法是在你的清單中定義,例如方案:

<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額外解析URL和WebView

這裏打開它也是有趣的帖子:http://danielmuller.asia/2013/02/android-open-an-app-from-web-link-or-fallback-to-market/

相關問題