Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Need some way to shorten this code

Tags:

java

android

I have this piece of code that I would like to shorten...

    PackageManager p = context.getPackageManager();
    final List<PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    PackageManager pro = context.getPackageManager();
    final List<PackageInfo> apllprovides = pro.getInstalledPackages(PackageManager.GET_PROVIDERS);

I am seriously irritated to do this again and again to add new flag permissions, and I need to do it a couple of times, is there a shorter method in which I could put all the flags on the same definition...???

Let me put it this way, can I do this...??? (of course this gives an error, but something similar..)

    PackageManager p = context.getPackageManager();
    final List<PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS).addFlag(PackageManager.GET_PROVIDERS);
like image 512
Shouvik Avatar asked Jul 19 '10 11:07

Shouvik


2 Answers

If it's the same syntax as C# and the flags are set properly, you could do this:

PackageManager p = context.getPackageManager(); 
final List<PackageInfo> appinstall = 
    p.getInstalledPackages(PackageManager.GET_PERMISSIONS | 
                                      PackageManager.GET_PROVIDERS)
like image 139
cjk Avatar answered Oct 17 '22 12:10

cjk


public void usegetPackageInfo(){
    // 
    final ListView lw = (ListView) findViewById(R.id.listView1);

    PackageManager p = lw.getContext().getPackageManager(); 
    final List<PackageInfo> appinstall = 
        p.getInstalledPackages(PackageManager.GET_PERMISSIONS | 
                                          PackageManager.GET_PROVIDERS);
    final TextView tw = (TextView) findViewById(R.id.textView1);
    Iterator it = appinstall.iterator();
    while (it.hasNext()) {
        PackageInfo rf = (PackageInfo) it.next();
        tw.append(rf.toString());

    }

}
like image 41
zhocker Avatar answered Oct 17 '22 10:10

zhocker