Java:需要一些方法来缩短此代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3280632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Need some way to shorten this code
提问by Shouvik
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);
采纳答案by cjk
If it's the same syntax as C# and the flags are set properly, you could do this:
如果它的语法与 C# 相同并且标志设置正确,则可以执行以下操作:
PackageManager p = context.getPackageManager();
final List<PackageInfo> appinstall =
p.getInstalledPackages(PackageManager.GET_PERMISSIONS |
PackageManager.GET_PROVIDERS)
回答by zhocker
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());
}
}
回答by Praveen
check the android Documentation: for permissions: http://developer.android.com/guide/topics/manifest/uses-permission-element.html
检查android文档:权限:http: //developer.android.com/guide/topics/manifest/uses-permission-element.html
for providers: http://developer.android.com/guide/topics/manifest/provider-element.html
对于提供者:http: //developer.android.com/guide/topics/manifest/provider-element.html
Before that Completely study about manifest documentation
在此之前 彻底研究清单文件

