我正在尝试将所有已安装的程序列表放入Spinner
. 它仅显示packageInfo.packageName
,但选定的项目应显示packageInfo.sourceDir
,MD5
并在下方微调器上显示SHA1
校验和。TextView
据我了解,这条线将信息存储在字典中。
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
以及在选择微调器项目时如何检索所有这些信息。
public class DisplayClass extends Activity implements AdapterView.OnItemSelectedListener{
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_layout);
//String tv;
TextView tv = findViewById(R.id.textView3);
tv.setMovementMethod( new ScrollingMovementMethod());
ArrayList<String> progArray = new ArrayList<String>();
Spinner spinner = findViewById(R.id.spinner);
for (ApplicationInfo packageInfo : packages) {
//Log.d(TAG, "Installed package :" + packageInfo.packageName);
//Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
String stringName = ("Package : " + packageInfo);
progArray.add(stringName);
//Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, progArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public <T extends View> T findViewById(int id) {
return super.findViewById(id);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner mySpinner=(Spinner) findViewById(R.id.spinner);
TextView tv = findViewById(R.id.selectedItem);
String text = mySpinner.getSelectedItem().toString();
//tv.append("Package : \n" + packageInfo.packageName+'\n');
//tv.append("Dir : \n" + packageInfo.sourceDir+'\n');
//File file = new File(packageInfo.sourceDir);
//String md5 = MD5.calculateMD5(file);
//String sha1 = SHA1.calculateSHA1(file);
//tv.setText("MD5 : \n" +md5 +"\n " );
//tv.append("SHA1 : \n" +sha1 +"\n \n" );
tv.setText(text);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}