简体   繁体   English

如何获取应用列表

[英]how to get list of apps

i got this code off the internet, i suppose to get a list of all the applications on the phone and judging by the comments on the webpage it works but i just cant seem to get it to work for me at all. 我从互联网上获取了此代码,我想获取手机上所有应用程序的列表,并根据其上起作用的网页上的评论来判断,但我似乎根本无法使其完全适用。 it seems to be the getPacketManager() that is giving me the problem. 似乎是getPacketManager()给了我这个问题。 can someone please look at the code and explain to me what i have to do to get it working please? 有人可以看看代码并向我解释如何使其正常工作吗?

this is my full class... i cant get it to even run, on the 3 locations where there is a "getPackageManager()" it is giving me an error on the left of the screen saying "The method getPackageManager() is undefined for the type getApps" 这是我的完整课程...我什至无法运行它,在3个有“ getPackageManager()”的位置上,这在屏幕左侧显示了一个错误,提示“方法getPackageManager()未定义对于类型getApps”

package cians.app;

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;

public class getApps{

class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
    System.out.print(appname + "\\t" + pname + "\\t" + versionName + "\\t"            +versionCode + "\\t");
}
}

private void listPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
    apps.get(i).prettyPrint();
}
}

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();        
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    if ((!getSysPackages) && (p.versionName == null)) {
        continue ;
    }
    PInfo newInfo = new PInfo();
    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);
}
return res; 
}}

getPackageManager() is a method of the 'Context' class, so you can call it from any object that extends 'Context'. getPackageManager()是“上下文”类的方法,因此您可以从扩展“上下文”的任何对象中调用它。 The class Activity extends context, and your main class extends Activity , so you are able to call getPackageManager() from your main class object. Activity类扩展了上下文,而主类扩展了Activity ,因此您可以从主类对象调用getPackageManager() If your class doesn't extend Context (that's the case of the getApps class), you can't call getPackageManager() from it. 如果您的类没有扩展ContextgetApps类就是这种情况),则无法从中调用getPackageManager() You'll need to get your activity context first. 您需要首先获取活动上下文。

EDIT: 编辑:

OK, you need to pass your activity to this class: 好的,您需要将您的活动传递给此类:

in getApps class add 在getApps类中添加

private Context parent = null;
public getApps(Context _parent) // a constructor that receives the context as parameter
{
   parent = _parent;
}

public String getInfo() { // your implementation here... }

and change getPackageManager()... to parent.getPackageManager()... 并将getPackageManager()...更改为parent.getPackageManager()...

in your main activity, create an object of getApps and then call it to get the info: 在您的主要活动中,创建一个getApps对象,然后调用它以获取信息:

getApps appsGetter = new getApps(this); // "this" is your activity actually,which extends Context
String info = appsGetter.getinfo();

now you still need to implement getInfo(), but it shouldn't be so hard. 现在,您仍然需要实现getInfo(),但这并不难。 and one more thing, classes in Java start with capital letter, so instead getApps consider calling it GetApps or AppsGetter . 还有一件事,Java中的类以大写字母开头,因此getApps考虑将其GetAppsAppsGetter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM