如何使用getPackageManager?

问题描述 投票:0回答:4

为了在Android模拟器上显示已安装的应用程序,我尝试了此代码。它编译成功但不起作用。怎么了?

package pack.GetAllInstalledApplications;
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 GetAllInstalledApplicationsExample extends Activity {

 public  ArrayList <PackageInfoStruct> res = new ArrayList <PackageInfoStruct>();

 public ListView list;
 public String app_labels[];


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

        getPackages();

        list = (ListView)findViewById(R.id.ListView01);
        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();
        }

 }
private ArrayList
 <PackageInfoStruct> getPackages() {
    ArrayList
 <PackageInfoStruct> apps = getInstalledApps(false);
    final int max = apps.size();
    for (int i=0; i < max; i++) {
        apps.get(i);
    }
    return apps;
}

private ArrayList
  <PackageInfoStruct> getInstalledApps(boolean 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;
}
    }  
  class PackageInfoStruct {             
 String appname = "";           
 String pname = "";             
 String versionName = "";      
 int versionCode = 0;           
 Drawable icon;                 
  }
android package-managers
4个回答
0
投票

在你对list.setAdapter的调用中,你应该使用不同的布局。我试过simple_list_item_1

list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, app_labels));

然后它似乎正确显示项目。


0
投票

在呈现应用标签之前,您应该添加一些日志,以了解您是否实际检索应用。验证然后你可以处理适配器。

请记住,ArrayAdapter的构造函数将第二个参数作为文本视图的资源ID(否则崩溃),因此请确保这一点。

此外,为了丢弃系统包,我使用这行代码:

... if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!= 0)继续; ...

其中pi是PackageInfo。

如果有效,请告诉我


0
投票

我更改了适配器布局,在代码上使用android.R.layout.simple_list_item_1,它可以工作。


-1
投票

这可能是您的列表视图的问题。您可以创建自定义ListView项目,它具有ImageView和文本视图,以显示包装可绘制图像和包名称。

创建一个列表项 - list_single.xml

将此xml文件放在资源/布局中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal" >
 <ImageView
 android:id="@+id/icon"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginBottom="5dp"
 android:layout_marginLeft="5dp"
 android:layout_marginRight="5dp"
 android:layout_marginTop="5dp"
 android:src="@drawable/ic_launcher" />
 <TextView
 android:id="@+id/Itemname"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="20sp" 
 android:paddingTop="5dp"/>
</LinearLayout>

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="{relativePackage}.${activityClass}" >

 <ListView
 android:id="@+id/android:list"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
</RelativeLayout>

最后,您的MainActivity.java文件

public class MainActivity extends ListActivity {

 String[] itemname ={
 "Safari",
 "Camera",
 "Global",
 "FireFox",
 "UC Browser",
 "Android Folder",
 "VLC Player",
 "Cold War"
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 this.setListAdapter(new ArrayAdapter<String>(
 this, R.layout.mylist,
 R.id.Itemname,itemname));
 }
}

注意:请注意,在此示例中,您使用自己定义的布局,而不是使用其中一种默认布局类型:

this.setListAdapter(new ArrayAdapter<String>(
 this, R.layout.mylist,
 R.id.Itemname,itemname));
© www.soinside.com 2019 - 2024. All rights reserved.