当前位置: 代码迷 >> Android >> Android 怎么在Activity中启动另一应用程序的方法,无需得到类名
  详细解决方案

Android 怎么在Activity中启动另一应用程序的方法,无需得到类名

热度:103   发布时间:2016-05-01 19:21:46.0
Android 如何在Activity中启动另一应用程序的方法,无需得到类名

?

在网上搜索了一会相关的实现代码,发现所有的文章都说是需要包名和类名。但是人家的程序,我们怎么可能知道哪个是第一个启动的Activity?所以,真正用在项目上,那种方法基本上没什么用的。于是查看官方文档,发现这样一个方法:?

Java代码??收藏代码
  1. public?abstract?Intent?getLaunchIntentForPackage?(String?packageName)??


英文原文:Return a “good” intent to launch a front-door activity in a package, for use for example to implement an “open” button when browsing through packages.大概意思就是返回一个程序入口的Intent,就是Java程序的Main方法。?
这下简单了,直接startActivity(返回的intent)即可。?
Java代码??收藏代码
  1. import?java.util.ArrayList;??
  2. import?java.util.HashMap;??
  3. import?java.util.List;??
  4. ???
  5. import?android.app.Activity;??
  6. import?android.content.Intent;??
  7. import?android.content.pm.PackageInfo;??
  8. import?android.content.pm.PackageManager;??
  9. import?android.os.Bundle;??
  10. import?android.view.View;??
  11. import?android.widget.AdapterView;??
  12. import?android.widget.ListView;??
  13. import?android.widget.AdapterView.OnItemClickListener;??
  14. ???
  15. public?class?Main?extends?Activity?{??
  16. ????/**?Called?when?the?activity?is?first?created.?*/??
  17. ????ListView?lv;??
  18. ????Adapter?adapter;??
  19. ????ArrayList<HashMap<String,?Object>>?items?=?new?ArrayList<HashMap<String,?Object>>();??
  20. ????PackageManager?pm;??
  21. ???
  22. ????@Override??
  23. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  24. ????????super.onCreate(savedInstanceState);??
  25. ????????setContentView(R.layout.main);??
  26. ????????lv?=?(ListView)?findViewById(R.id.lv);??
  27. ????????final?PackageManager?pm?=?getPackageManager();??
  28. ????????//?得到PackageManager对象??
  29. ????????List<PackageInfo>?packs?=?pm??
  30. ????????????????.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);??
  31. ????????//?得到系统?安装的所有程序包的PackageInfo对象??
  32. ???
  33. ????????for?(PackageInfo?pi?:?packs)?{??
  34. ????????????HashMap<String,?Object>?map?=?new?HashMap<String,?Object>();??
  35. ????????????map.put("icon",?pi.applicationInfo.loadIcon(pm));??
  36. ????????????//?图标??
  37. ????????????map.put("appName",?pi.applicationInfo.loadLabel(pm));??
  38. ????????????//?应用名??
  39. ????????????map.put("packageName",?pi.packageName);??
  40. ????????????//?包名??
  41. ????????????items.add(map);??
  42. ????????????//?循环读取存到HashMap,再增加到ArrayList.一个HashMap就是一项??
  43. ????????}??
  44. ???
  45. ????????adapter?=?new?Adapter(this,?items,?R.layout.piitem,?new?String[]?{??
  46. ????????????????"icon",?"appName",?"packageName"?},?new?int[]?{?R.id.icon,??
  47. ????????????????R.id.appName,?R.id.packageName?});??
  48. ????????//?参数:Context,ArrayList(item的集合),item的layout,包含ArrayList中Hashmap的key的数组,key所对应的值相对应的控件id??
  49. ????????lv.setAdapter(adapter);??
  50. ????????lv.setOnItemClickListener(new?OnItemClickListener()?{??
  51. ???
  52. ????????????@Override??
  53. ????????????public?void?onItemClick(AdapterView<?>?parent,?View?view,??
  54. ????????????????????int?position,?long?id)?{??
  55. ????????????????//?TODO?Auto-generated?method?stub??
  56. ????????????????String?packageName?=?(String)?items.get(position).get(??
  57. ????????????????????????"packageName");??
  58. ????????????????//取到点击的包名??
  59. ????????????????Intent?i?=?pm.getLaunchIntentForPackage(packageName);??
  60. ????????????????//如果该程序不可启动(像系统自带的包,有很多是没有入口的)会返回NULL??
  61. ????????????????if?(i?!=?null)??
  62. ????????????????????startActivity(i);??
  63. ????????????}??
  64. ???
  65. ????????});??
  66. ???
  67. ????}??
  68. ???
  69. }??

欢迎指教!
  相关解决方案