Java:需要一些方法来缩短这段代码

时间:2010-07-19 11:31:11

标签: java android

我想要缩短这段代码......

    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);

我非常恼火地反复这样做以添加新的标志权限,我需要做几次,是否有一个更短的方法,我可以将所有标志放在相同的定义上... ???

让我这样说,我可以这样做...... ??? (当然这会产生错误,但类似的东西......)

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

3 个答案:

答案 0 :(得分:9)

如果它与C#的语法相同并且标志设置正确,则可以这样做:

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

答案 1 :(得分:2)

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());

    }

}

答案 2 :(得分:0)