public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView internetapps= (ListView) findViewById(R.id.list);
ArrayList myList = new ArrayList();
PackageManager pm = this.getPackageManager();
List<PackageInfo> applist= pm.getInstalledPackages(0);
Iterator<PackageInfo> it= applist.iterator();
while (it.hasNext()){
PackageInfo pk= (PackageInfo)it.next();
if(PackageManager.PERMISSION_GRANTED==(pm.checkPermission(Manifest.permission.INTERNET, pk.packageName))) //checking if the package is having INTERNET permission
{
myList.add(""+pk.applicationInfo.loadLabel(pm));
}
}
internetapps.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myList));
final TextView text1 = (TextView) findViewById(R.id.text2);
internetapps.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View arg1, int list_position,long app_Uid)
{
String app_selected=parent.getItemAtPosition(list_position).toString();
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
long UID;
//loop through the list of installed packages and see if the selected
//app is in the list
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(app_selected)){
//get the UID for the selected app
UID = packageInfo.uid;
text1.setText("Yeah");
}
else {
text1.setText("No");
}
}
}
});
}
如何从列表中的应用程序中获取应用程序的 uid。我尝试使用上面的代码获取 uid,但它总是显示否定结果(我使用 setText 来跟踪它在 if..else 中运行的代码......在我点击一个项目后它总是显示“No”) . 我的代码有什么问题。