2011-11-16 140 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

相關問題