问题描述
我得到这个code了互联网,我想拿到手机上的所有应用程序的列表,并通过网页上的评论来看它的工作原理,但我只是不能似乎得到它在为我工作所有。这似乎是给我的问题getPacketManager()。有人可以看一下code和向我解释什么,我必须做的就是它的工作好吗?
这是我的满级...我不能让它甚至运行,在3个地方,那里是一个getPackageManager()它给我一个错误在屏幕的左边说:该方法getPackageManager( )是未定义的类型getApps
包cians.app;进口的java.util.ArrayList;
进口的java.util.List;进口android.app.Activity;
进口android.content.pm.PackageInfo;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类getApps {类PINFO {
私人字符串APPNAME =;
私人字符串PNAME =;
私人字符串的versionName =;
私人诠释版本code = 0;
私人可绘制图标;
私人无效prettyPrint(){
System.out.print(APPNAME +\\\\ T+ PNAME +\\\\ T+ +的versionName\\\\ T+版本code +\\\\ T);
}
}私人无效listPackages(){
ArrayList的<&PINFO GT;应用= getInstalledApps(假); / * FALSE =没有系统包* /
最终诠释最大= apps.size();
的for(int i = 0; I<最大;我++){
apps.get(ⅰ)prettyPrint();
}
}私人的ArrayList<&PINFO GT; getInstalledApps(布尔getSysPackages){
ArrayList的<&PINFO GT; RES =新的ArrayList<&PINFO GT;();
清单< PackageInfo>包= getPackageManager()getInstalledPackages(0)。
的for(int i = 0; I< packs.size();我++){
PackageInfo p值= packs.get(ⅰ);
如果((getSysPackages)及!及(p.versionName == NULL)){
继续;
}
PINFO newInfo =新PINFO();
。newInfo.appname = p.applicationInfo.loadLabel(getPackageManager())的toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.version code = p.version code;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
返回水库;
}}
getPackageManager()
是上下文类的方法,所以你可以从任何调用它对象扩展语境。类活动
扩展的背景下,和你的主类扩展了活动
,所以你可以调用 getPackageManager()
从你的主类对象。如果你的类没有延伸上下文
(这是 getApps
类的情况下),你不能叫 getPackageManager()
从它。你需要先得到你的活动范围内。
的编辑:的
OK,您需要将您的活动传递给这个类:
在getApps类添加
私人语境父= NULL;
公共getApps(上下文_parent)//接收上下文参数的构造函数
{
父= _parent;
}公共字符串的getInfo(){//你的实现在这里...}
和变化 getPackageManager()...
到 parent.getPackageManager()...
在您的主要活动,创建getApps的对象,然后调用它来获取信息:
getApps appsGetter =新getApps(本); //这其实是你的活动,它扩展了上下文
字符串信息= appsGetter.getinfo();
现在你仍然需要实现的getInfo(),但它不应该是这么难。还有一件事,在Java类与大写字母开头,所以不是 getApps
考虑调用它 GetApps
或 AppsGetter
。
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. 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"
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'. The class Activity
extends context, and your main class extends Activity
, so you are able to call getPackageManager()
from your main class object. If your class doesn't extend Context
(that's the case of the getApps
class), you can't call getPackageManager()
from it. You'll need to get your activity context first.
EDIT:
OK, you need to pass your activity to this class:
in getApps class add
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()...
in your main activity, create an object of getApps and then call it to get the info:
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. and one more thing, classes in Java start with capital letter, so instead getApps
consider calling it GetApps
or AppsGetter
.
这篇关于如何让应用程序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!