我正在开发一个小应用程序,它将已安装的应用程序显示为微调器中的列表.只有应用程序名称显示为微调器值.每当我从微调器中选择一个应用程序时,我需要检索所选应用程序的UID.我该如何实现这个功能?
appspinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent,View arg1,int app_pos,long app_id) { // TODO Auto-generated method stub String app_selected=parent.getItemAtPosition(app_pos).toString(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub return; } });
解决方法
您需要使用PackageManager来获取有关您在列表中选择的任何应用程序的包信息.我没有使用Spinner完成此操作,但我确信它应该与我在ListView中的工作方式相同.
appspinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent,long app_id) { // TODO Auto-generated method stub String app_selected=parent.getItemAtPosition(app_pos).toString(); final PackageManager pm = getPackageManager(); //get a list of installed apps. List<ApplicationInfo> packages = pm.getInstalledApplications( PackageManager.GET_Meta_DATA); int 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; break; //found a match,don't need to search anymore } } //Do whatever with the UID Log.i("Check UID","UID is: " + UID); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub return; } });
您可能想要查看packageName如何返回packageName,以便您可以尝试将其与所选的任何内容进行匹配.
希望这能指出你正确的方向并帮助你.祝好运.