0

I am using the following code to list all installed activities but instead of showing the app name it shows the name like com.xyz.xyz.dialer I want it to show app name only. Where do I need to change and to what?

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)

This is the getInstalledActivities method. I have seperated it here for easy access.

   /*
 * 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()    
4

1 回答 1

0

实际上,在您的系统中,应用程序以其唯一的包名称存储,这就是您将它们与它们一起列出的原因。如果您只想要该名称的最后一部分(假设您没有重复项),您可以做一些技巧:

String s = "com.example.MyApp";
String appName = s.substring(s.lastIndexOf(".")+1); // appName <- MyApp
于 2013-06-29T14:16:01.200 回答