2011-11-28 22 views
2

我正在開發一個應用程序,需要用戶在其手機上安裝的應用程序列表。 我已經看過在線教程,並沒有一個足夠具體,即顯示代碼,但從來沒有顯示在哪裏把它需要幫助獲取Android手機上安裝的所有應用程序列表

但是,編程這麼新,我不能用這個,因爲我不知道在哪裏把它在我的代碼中,儘管我找到了一個可以工作的代碼,它不顯示版本或圖標。我真的可以用一個人來握住我的手。

我需要具體的是 arraylist顯示用戶安裝的應用程序,圖標和版本。

這裏是我工作的代碼:

包com.fina;

import java.util.ArrayList; 
    import java.util.List; 
    import android.app.Activity; 
    import android.content.pm.PackageInfo; 
    import android.graphics.drawable.Drawable; 
    import android.os.Bundle; 

    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.Toast; 


    public class scan extends Activity { 

公共的ArrayList 解析度=新的ArrayList ();公共ListView列表; public String app_labels [];

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.allc);

   getPackages(); 

     list = (ListView)findViewById(R.id.listView1); 
       try{ 
      list.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_dropdown_item_1line,     app_labels));   }catch(Exception e){ 
      System.out.println("Err ++> " + e.getMessage()); 
      Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
     } 

} 私人的ArrayList getPackages(){ArrayList的 應用 = getInstalledApps(假); final int max = apps.size();對於(int i = 0; i < max; i ++){ (apps.get } return apps; }

私人的ArrayList getInstalledApps(布爾getSysPackages){

List 
    <PackageInfo> packs = getPackageManager().getInstalledPackages(0); 
    try{ 
     app_labels = new String[packs.size()]; 
    }catch(Exception e){    Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
    } 
    for(int i=0;i < packs.size();i++) { 
     PackageInfo p = packs.get(i); 
     if ((!getSysPackages) && (p.versionName == null)) { 
      continue ; 
     } 
     PackageInfoStruct newInfo = new PackageInfoStruct(); 
     newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); 
     newInfo.pname = p.packageName; 
     newInfo.versionName = p.versionName; 
     newInfo.versionCode = p.versionCode; 
     newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); 
     res.add(newInfo); 

     app_labels[i] = newInfo.appname; 
    } 
    return res;  } 
    } 
    //This class is for storing the data for each application 
    class PackageInfoStruct { 
     String appname = ""; 
     String pname = ""; 
     String versionName = ""; 
     int versionCode = 0; 
     Drawable icon; 
    } 

這裏是我的allc.xml鏈接到它:

<AbsoluteLayout 
    android:id="@+id/absoluteLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/allcast" 
     android:layout_width="61dp" 
     android:layout_height="66dp" 
     android:layout_x="0dp" 
     android:layout_y="0dp" 
     android:background="@drawable/invis" 
     /> 


    <Button 
     android:id="@+id/downcast" 
     android:layout_width="63dp" 
     android:layout_height="60dp" 
     android:layout_x="64dp" 
     android:layout_y="3dp" 
     android:background="@drawable/invis" 
     /> 

    <Button 
     android:id="@+id/commcast" 
     android:layout_width="51dp" 
     android:layout_height="57dp" 
     android:layout_x="255dp" 
     android:layout_y="3dp" 
     android:background="@drawable/invis" /> 



    <Button 
     android:id="@+id/achcast" 
     android:layout_width="62dp" 
     android:layout_height="58dp" 
     android:layout_x="192dp" 
     android:layout_y="2dp" 
     android:background="@drawable/invis" 
     /> 


    <Button 
     android:id="@+id/recocast" 
     android:layout_width="59dp" 
     android:layout_height="60dp" 
     android:layout_x="128dp" 
     android:layout_y="3dp" 
     android:background="@drawable/invis" /> 


    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_x="0dp" 
     android:layout_y="71dp" > 

    </ListView> 

</AbsoluteLayout> 

請儘可能詳細地回答您的問題,而不僅僅是一個鏈接到我可以使用的代碼網站,我真的需要有人來牽着我的手。

+0

的[獲取Android中安裝的應用程序的列表(可能的複製http://stackoverflow.com/questions/ 9393676/GET-A-列表中安裝的應用程序功能於機器人) –

回答

相關問題