转自:http://lfcaolibin.iteye.com/blog/618663
PackageManager()的使用列举相关的事件
文章分类:移动开发
看来这个东西应该专门开一个帖子了,发现这个包里面的东西太多,以前写的很零碎不容易找
查询系统使用的Content Providers
Java代码
1. for (PackageInfo pack : getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) {
2. ProviderInfo[] providers = pack.providers;
3. if (providers != null) {
4. for (ProviderInfo provider : providers) {
5. Log.d("Example", "provider: " + provider.authority);
6. }
7. }
8. }
Java代码
1. for (PackageInfo pack : getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) {
2. ProviderInfo[] providers = pack.providers;
3. if (providers != null) {
4. for (ProviderInfo provider : providers) {
5. Log.d("Example", "provider: " + provider.authority);
6. }
7. }
8. }
for (PackageInfo pack : getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
Log.d("Example", "provider: " + provider.authority);
}
}
}
Java代码
1. class PInfo {
2. private String appname = "";
3. private String pname = "";
4. private String versionName = "";
5. private int versionCode = 0;
6. private Drawable icon;
7. private void prettyPrint() {
8. log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
9. }
10. }
11.
12. private void listPackages() {
13. ArrayList
14. final int max = apps.size();
15. for (int i=0; i
16. apps.get(i).prettyPrint();
17. }
18. }
19.
20. private ArrayList
21. ArrayList
22. List
23. for(int i=0;i
24. PackageInfo p = packs.get(i);
25. if ((!getSysPackages) && (p.versionName == null)) {
26. continue ;
27. }
28. PInfo newInfo = new PInfo();
29. newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
30. newInfo.pname = p.packageName;
31. newInfo.versionName = p.versionName;
32. newInfo.versionCode = p.versionCode;
33. newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
34. res.add(newInfo);
35. }
36. return res;
37. }
Java代码
1. class PInfo {
2. private String appname = "";
3. private String pname = "";
4. private String versionName = "";
5. private int versionCode = 0;
6. private Drawable icon;
7. private void prettyPrint() {
8. log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
9. }
10. }
11.
12. private void listPackages() {
13. ArrayList
14. final int max = apps.size();
15. for (int i=0; i
16. apps.get(i).prettyPrint();
17. }
18. }
19.
20. private ArrayList
21. ArrayList
22. List
23. for(int i=0;i
24. PackageInfo p = packs.get(i);
25. if ((!getSysPackages) && (p.versionName == null)) {
26. continue ;
27. }
28. PInfo newInfo = new PInfo();
29. newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
30. newInfo.pname = p.packageName;
31. newInfo.versionName = p.versionName;
32. newInfo.versionCode = p.versionCode;
33. newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
34. res.add(newInfo);
35. }
36. return res;
37. }
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
log(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode + "\t");
}
}
private void listPackages() {
ArrayList
final int max = apps.size();
for (int i=0; i
apps.get(i).prettyPrint();
}
}
private ArrayList
ArrayList
List
for(int i=0;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;
}
用这个方法来获取系统内所包含的Applications信息:
Java代码
1. private void listPackages() {
2.
3. ArrayList < PInfo > apps = getInstalledApps(false); /* false = no system packages */
4.
5. final int max = apps.size();
6.
7. for (int i=0; i < max; i++) {
8.
9. apps.get(i).prettyPrint();
10.
11. }
12. }
Java代码
1. private void listPackages() {
2.
3. ArrayList < PInfo > apps = getInstalledApps(false); /* false = no system packages */
4.
5. final int max = apps.size();
6.
7. for (int i=0; i < max; i++) {
8.
9. apps.get(i).prettyPrint();
10.
11. }
12. }
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();
}
}
Java代码
1. 01 PackageManager manager = this.getPackageManager();
2.
3. 02 try {
4.
5. 03
6.
7. 04 PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
8.
9. 05 String packageName = info.packageName;
10.
11. 06 int versionCode = info.versionCode;
12.
13. 07 String versionName = info.versionName;
14.
15. 08
16.
17. 09 } catch (NameNotFoundException e) {
18.
19. 10 // TODO Auto-generated catch block
20.
21. 11 }
Java代码
1. 01 PackageManager manager = this.getPackageManager();
2.
3. 02 try {
4.
5. 03
6.
7. 04 PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
8.
9. 05 String packageName = info.packageName;
10.
11. 06 int versionCode = info.versionCode;
12.
13. 07 String versionName = info.versionName;
14.
15. 08
16.
17. 09 } catch (NameNotFoundException e) {
18.
19. 10 // TODO Auto-generated catch block
20.
21. 11 }