2013-06-29 42 views
0

我使用下面的代碼來列出所有已安裝的活動,但不是顯示應用程序名稱,而是顯示名稱,如com.xyz.xyz.dialer 我希望它只顯示應用程序名稱。我需要改變什麼?如何在代碼中列出活動而不是軟件包名稱?

public class AppPicker extends ListActivity{ 

// Buffer used to store package and class information, and also determine the number of installed activities 
private ArrayList<String[]> _activitiesBuffer = null; 

// Buffers for package and class information 
private String[] _packages = null; 
private String[] _classes = null; 

// Index used to fill buffers 
private int _index = 0; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Get all installed activities (package and class information for every activity) 
    getAllInstalledActivities();    

    // Set content to GUI 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, _classes)); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    // Add listener 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      // When clicked, show a toast with the selected activity 
      Toast.makeText(
       getApplicationContext(), 
       ((TextView) view).getText(), 
       Toast.LENGTH_SHORT).show(); 

      // When clicked, start selected activity, if allowed or possible 
      try { 

       Intent intent = new Intent().setClassName(
         _packages[position], // package 
         _classes[position]); // class 
       startActivity(intent); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } // public void onItemClick(AdapterView<?> parent, View view, int position, long id) 

    }); 

} // public void onCreate(Bundle savedInstanceState) 

這是getInstalledActivities方法。我在這裏分離它以便於訪問。

/* 
* Get all installed activities 
*/ 
private void getAllInstalledActivities() { 

    // Initialize activities buffer 
    _activitiesBuffer = new ArrayList<String[]>(); 

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

    Iterator<ResolveInfo> iterator1 = pkgAppsList.iterator(); 
    while (iterator1.hasNext()) { 

     ResolveInfo resolveInfo = iterator1.next(); 

     String[] buf = new String[] { 
       resolveInfo.activityInfo.packageName, 
       resolveInfo.activityInfo.name}; 

     _activitiesBuffer.add(buf); 

    } // while (iterator1.hasNext()) 

    _packages = new String[_activitiesBuffer.size()]; 
    _classes = new String[_activitiesBuffer.size()]; 

    Iterator<String[]> iterator2 = _activitiesBuffer.iterator(); 
    while (iterator2.hasNext()) { 

     String[] buf = iterator2.next(); 

     // Store package information 
     _packages[_index] = buf[0]; 

     // Store class information 
     _classes[_index] = buf[1]; 

     _index++; 

    } // while (iterator2.hasNext()) 

} // private void getAllInstalledActivities()  

回答

0

在系統中實際應用存儲與這就是爲什麼你與他們列出他們自己獨特的包名。如果你只想要這個名字的最後一部分(假設你沒有重複),你可以做一些技巧:

String s = "com.example.MyApp"; 
String appName = s.substring(s.lastIndexOf(".")+1); // appName <- MyApp 
+0

我認爲這是沒有必要,MyApp是應用程序的名稱。我剛剛瀏覽了應用程序,包名稱和應用程序名稱似乎有所不同。你能告訴我可以改變我發佈的上述代碼嗎? –

+0

嘗試'ResolveInfo resolveInfo = iterator1.next(); System.out.println(「label =」+ resolveInfo.loadLabel(getPackageManager()));'如果loadLabel給出你想要的字符串,查看你的Logcat。如果我已經理解你想要的東西,你需要在'res/strings.xml'中獲取超過包管理器可檢索的包名稱 – Rob013

+0

沒有做任何事情。還是一樣 –

相關問題