2017-07-31 54 views
0

我發現了一個問題here,但它會檢查包名。我想要的是檢查一個網址(例如https://www.facebook.comhttps://www.twitter.com)並在其本機應用程序中打開它(如果已安裝),或者在未安裝的情況下在瀏覽器中打開它。如何在原生應用程序中而不是瀏覽器中打開url?

String url = "https://www.facebook.com"; 

if (hasNativeApp()) { 
    // open in native app 
} else { 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} 
+0

當您在安裝應用程序時啓動'ACTION_VIEW'會發生什麼?你嘗試過'http'而不是'https'嗎? – JimmyB

+0

包名是標識App所必需的。您無法從鏈接中檢查已安裝的天氣應用程序。 –

+0

使用webview並設置其設置 –

回答

0

試試這個檢查已安裝的應用程序或不

Intent linkedinIntent = new Intent(Intent.ACTION_SEND); 
linkedinIntent.putExtra(Intent.EXTRA_TEXT, "DATA"); 
linkedinIntent.setType("text/plain"); 
boolean linkedinAppFound = false; 

List<ResolveInfo> matches2 = this.getPackageManager() 
     .queryIntentActivities(linkedinIntent, 0); 

for (ResolveInfo info : matches2) { 
    if (info.activityInfo.packageName.toLowerCase().startsWith(
      "com.linkedin")) { 
     linkedinIntent.setPackage(info.activityInfo.packageName); 
     linkedinAppFound = true; 
     break; 
    } 
} 
if (linkedinAppFound) { 
    Toast.makeText(MainActivity.this, "app found", Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(MainActivity.this, "app not found", Toast.LENGTH_LONG).show();  
} 

試試這個讓所有安裝的應用程序

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities(mainIntent, 0); 
+0

通過這樣做,我們需要事先知道包名,對吧?如果我事先不知道鏈接和包名,會發生什麼情況? – Sam

+0

@Sam check updated ans –

0

您不能檢查的天氣名單應用程序安裝或不從它的鏈接。包名是標識App所必需的。

要檢查App安裝或不使用以下功能。

public boolean appInstalledOrNot(Context context, String paramString) { 
    PackageManager localPackageManager = context.getPackageManager(); 
    try { 
     //noinspection WrongConstant 
     localPackageManager.getPackageInfo(paramString, 1); 
     return true; 
    } catch (PackageManager.NameNotFoundException ignored) { 
    } 
    return false; 
} 

嘗試此開放原生應用

String facebookPackage = "com.facebook.katana"; 

if (appInstalledOrNot(this,facebookPackage)) { 
    String url = "https://m.facebook.com"; 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} else { 
    String url = "https://www.facebook.com"; 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} 

注:,如果你正在尋找的Facebook的包名和Twitter

com.facebook.katana - Facebook的

com.twitter.android - Twtter

+0

如果事先不能知道url鏈接,會發生什麼情況?維護所有應用程序的數據庫以檢查本機應用程序是否可用是不可能的。那麼,如果可用,在默認情況下,如果瀏覽器不可用,則無法在本機應用程序中打開鏈接? – Sam

+0

在我的知識..沒有..這是不可能的。應用程序可以用它的包名稱來標識。 –

+0

只要你擔心包名。你可以隨時從特定應用程序的Play商店鏈接中獲取它。 –

相關問題