2012-01-27 82 views
2

我正在編寫一個Android應用程序,一個塔斯克其他應用程序,我真的不覺得獲得另一個應用程序的標籤的方式。這是我的觀點,我使用ListActivity來選擇已安裝的應用程序,然後當您單擊它時創建Intent和所有應用程序的東西,我想要做的就是嚮應用程序展示android:label選擇,我在ResolverInfo中找到一個名爲.activityInfo.labelRes的屬性,我認爲是用戶選擇的應用程序的R類的標籤描述符,有無論如何獲得匹配該ID的字符串? 謝謝! D.Gómez獲得了android:標籤從

回答

4

您可以使用loadLabel(PackageManager)方法ResolveInfo來獲取活動的標籤。這裏是指找到該設備上的所有發射活動,並將它們打印到logcat的一個完整的例子:

// Get the package manager 
PackageManager pm = getPackageManager(); 
// Create an intent that matches all launcher activities 
// (and ignores non-launcher activities) 
Intent launcherIntent = new Intent(Intent.ACTION_MAIN); 
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

// Get all activites matching the intent 
List<ResolveInfo> launchers = pm.queryIntentActivities(launcherIntent, 0); 

for(ResolveInfo info : launchers) { 
    // Get the activity label and print it 
    CharSequence label = info.loadLabel(pm); 
    Log.v("LabelTest", "App found: " + label); 
} 

爲了回答你問題的第二部分也是如此,有關訪問應用程序的資源:他們可以通過調用訪問getPackageManager().getResourcesForApplication(String),這將返回一個Resources對象,您可以使用,但在你的情況下,這不應該是必要的。

+0

謝謝!這樣可行! – DGomez 2012-01-27 19:53:44

+0

好知道我可以訪問其他應用程序的所有資源,是一個更通用的解決問題的方法,我在機器人編程新我不是很用API熟悉呢,謝謝你的答案! – DGomez 2012-01-27 20:02:10