2011-11-16 104 views
0

我目前正在开发一个应用程序,允许用户选择一个应用程序并在以后启动它(功能更多,但这是我所拥有的主要功能一个问题。)在Android上获取并显示已安装程序的列表

我正在寻找一种方式来获取应用程序的列表(用户安装或可更新,例如Gmail,GMaps,等等...),并把它扔进AlertDialog类似于你如何添加主屏幕的快捷方式(长按 - >应用程序)。

This是我使用的线程有代码来获取我需要的应用程序列表。但是,我将如何将它变成AlertDialog?

这是来自线程的代码。

public void getApps() 
{ 
    PackageManager pm = getPackageManager(); 
    List<ApplicationInfo> apps = pm.getInstalledApplications(0); 
    List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>(); 

    for(ApplicationInfo app : apps) { 
     //checks for flags; if flagged, check if updated system app 
     if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) { 
      installedApps.add(app); 
     //it's a system app, not interested 
     } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { 
      //Discard this one 
     //in this case, it should be a user-installed app 
     } else { 
      installedApps.add(app); 
     } 
    } 
}//end getApps() 

这里是我用来显示类似于我想使用的AlertDialog的代码。

//PseudoCode does not compile 
public void displayAppList(View v) 
{ 
    final CharSequence[] items = {getApps()}; 

    AlertDialog.Builder builder = new AlertDialog.Builder(SchedulerActivity.this); 
    builder.setTitle("Choose an App To Launch"); 
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      appChoiceString[count] = items[item]; 
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    builder.setPositiveButton("Yes", 
    new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     Toast.makeText(SchedulerActivity.this, "Success", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    builder.setNegativeButton("No", 
    new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     Toast.makeText(SchedulerActivity.this, "Fail", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

任何帮助让它正确显示将是非常棒的。

回答

3

为什么不使用标准意向选择器? (见this)。否则,你可能想要解释什么不是你想要的方式,以及你真正希望看到的细节。

+0

此刻我什么也没有显示。我有一个所有用户安装/可更新的应用程序列表(见上文),我想找出一种方法将它放入一个AlertDialog(列表单选按钮和一个确定/取消按钮)。我从来没有使用意图,每次我试图阅读的文章,它只是似乎对我没有实际的示例代码乱码。 – Cistoran

+0

为了启动应用程序等,你将不得不在某些时候理解意图。这是Android应用程序工作的基础部分。你可能已经看到了这个,但是这个文档可能是一个很好的开始:http://developer.android.com/guide/topics/intents/intents-filters.html – kabuko

+0

我其实从来没有见过。感谢您的链接。 – Cistoran

相关问题