我使用以下代码列出所有已安装的活动,但不显示应用名称,而是显示com.xyz.xyz.dialer等名称 我希望它只显示应用名称。我需要在哪里改变什么?
public class AppPicker extends ListActivity{
// Buffer used to store package and class information, and also determine the number of installed activities
private ArrayList<String[]> _activitiesBuffer = null;
// Buffers for package and class information
private String[] _packages = null;
private String[] _classes = null;
// Index used to fill buffers
private int _index = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get all installed activities (package and class information for every activity)
getAllInstalledActivities();
// Set content to GUI
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, _classes));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
// Add listener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the selected activity
Toast.makeText(
getApplicationContext(),
((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
// When clicked, start selected activity, if allowed or possible
try {
Intent intent = new Intent().setClassName(
_packages[position], // package
_classes[position]); // class
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
} // public void onItemClick(AdapterView<?> parent, View view, int position, long id)
});
} // public void onCreate(Bundle savedInstanceState)
这是getInstalledActivities方法。我把它分开来便于访问。
/*
* Get all installed activities
*/
private void getAllInstalledActivities() {
// Initialize activities buffer
_activitiesBuffer = new ArrayList<String[]>();
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities( intent, 0);
Iterator<ResolveInfo> iterator1 = pkgAppsList.iterator();
while (iterator1.hasNext()) {
ResolveInfo resolveInfo = iterator1.next();
String[] buf = new String[] {
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name};
_activitiesBuffer.add(buf);
} // while (iterator1.hasNext())
_packages = new String[_activitiesBuffer.size()];
_classes = new String[_activitiesBuffer.size()];
Iterator<String[]> iterator2 = _activitiesBuffer.iterator();
while (iterator2.hasNext()) {
String[] buf = iterator2.next();
// Store package information
_packages[_index] = buf[0];
// Store class information
_classes[_index] = buf[1];
_index++;
} // while (iterator2.hasNext())
} // private void getAllInstalledActivities()
答案 0 :(得分:0)
实际上,在您的系统中,应用程序以其唯一的包名存储,这就是您使用它们列出它们的原因。如果你只想要那个名字的最后一部分(假设你没有重复)你可以做一些技巧:
String s = "com.example.MyApp";
String appName = s.substring(s.lastIndexOf(".")+1); // appName <- MyApp