2012-03-12 55 views
2

感謝上次回覆,如何獲取相機應用程序包名稱

是否可以獲取安裝在設備上的相機應用程序的包名?如果操作系統是自定義的,則默認的軟件包名稱會被設備製造商更改。如何通過編碼獲取包名?我不確定這是可能的。

回答

6

我不知道,但我想你可以用得到應用的包名指定的意圖 ..

看看這段代碼用於獲取該處理的特定意圖可用的應用程序信息,

/** 
* Indicates whether the specified action can be used as an intent. This 
* method queries the package manager for installed packages that can 
* respond to an intent with the specified action. If no suitable package is 
* found, this method returns false. 
* 
* @param context The application's environment. 
* @param action The Intent action to check for availability. 
* 
* @return True if an Intent with the specified action can be sent and 
*   responded to, false otherwise. 
*/ 
public static boolean isIntentAvailable(Context context, String action) { 
    final PackageManager packageManager = context.getPackageManager(); 
    final Intent intent = new Intent(action); 
    List list = 
      packageManager.queryIntentActivities(intent, 
        PackageManager.MATCH_DEFAULT_ONLY); 
    return list.size() > 0; 
} 

現在使用攝像機意圖您可以獲得處理IMAGE_CAPTURE意圖的應用程序信息,並使用該信息可以輕鬆獲取包名。

更新:

在你的情況的特定意圖是

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

編輯:

List<ResolveInfo> listCam = packageManager.queryIntentActivities(intent, 0); 
for (ResolveInfo res : listCam) { 
    Log.e("Camera Application Package Name and Activity Name",res.activityInfo.packageName + " " + res.activityInfo.name)); 
} 

試試這個,讓我知道發生什麼事..

+0

謝謝,我得到了包名。 – RAAAAM 2012-03-12 10:44:16

+0

[ResolveInfo {4053ded8 com.mediatek.camera.Camera p = 0 o = 0 m = 0x108000}]這就是我編譯上面的代碼時得到的結果。我們如何分離包名本身? – RAAAAM 2012-03-12 10:49:04

+0

Simply res.activityInfo.packageName爲您提供軟件包名稱.. :-) – user370305 2012-03-12 10:51:12

0

看到http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon

class PInfo { 
    private String appname = ""; 
    private String pname = ""; 
    private String versionName = ""; 
    private int versionCode = 0; 
    private Drawable icon; 
    private void prettyPrint() { 
     Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode); 
    } 
} 

private ArrayList<PInfo> getPackages() { 
    ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */ 
    final int max = apps.size(); 
    for (int i=0; i<max; i++) { 
     apps.get(i).prettyPrint(); 
    } 
    return apps; 
} 

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) { 
    ArrayList<PInfo> res = new ArrayList<PInfo>();   
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); 
    for(int i=0;i<packs.size();i++) { 
     PackageInfo p = packs.get(i); 
     if ((!getSysPackages) && (p.versionName == null)) { 
      continue ; 
     } 
     PInfo newInfo = new PInfo(); 
     newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); 
     newInfo.pname = p.packageName; 
     newInfo.versionName = p.versionName; 
     newInfo.versionCode = p.versionCode; 
     newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); 
     res.add(newInfo); 
    } 
    return res; 
} 
0

我想這是不可能的,或者可能以一定精度。您可以枚舉所有系統軟件包(隨設備提供的應用程序)並嘗試選擇具有CAMERA權限的軟件包。但這種方法可以給你幾個應用程序,因爲幾個應用程序可以使用相機滿足他們的需求。但一般情況下,我猜這是不可能的。

8

你試過這個嗎?

PackageManager packman = getPackageManager(); 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    String pack = intent.resolveActivity(packman).getPackageName(); 
相關問題