Java的:需要一些方法来缩短这个code方法来、Java、code

2023-09-12 23:53:05 作者:在你婚礼上举麦唱忐忑

我有这块code,我想缩短...

  PackageManager P = context.getPackageManager();
    最后的名单,其中,PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    PackageManager亲= context.getPackageManager();
    最后的名单,其中,PackageInfo> apllprovides = pro.getInstalledPackages(PackageManager.GET_PROVIDERS);
 

我是认真恼火做到这一点一再增加新标志的权限,我需要做几次,有没有,我可以把所有的国旗相同的定义更短的方法... ???

让我这么说吧,我能做到这一点???? (当然这给出了一个错误,但类似的东西..)

  PackageManager P = context.getPackageManager();
    最后的名单,其中,PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS).addFlag(PackageManager.GET_PROVIDERS);
 
Java on Visual Studio Code的更新 – 2021年7月

解决方案

如果它是相同的语法,C#和标志设置是否正确,你可以这样做:

  PackageManager P = context.getPackageManager();
最后的名单,其中,PackageInfo> appinstall =
    p.getInstalledPackages(PackageManager.GET_PERMISSIONS |
                                      PackageManager.GET_PROVIDERS)
 

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

解决方案

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)