Android 通过包名打开App的代码
做launcher时,用户点击apk的图标就对应着需要打开这个apk,有两种方式可以启动这个apk
第一种:知道apk的包名和它的主Activity
- // 帮助
- private ComponentName help_set;
- private final static String help_set_pack = "cn.abc.help";
- private final static String help_set_name = "cn.abc.help.MainActivity";
- /**
- * 启动一个app
- * com -- ComponentName 对象,包含apk的包名和主Activity名
- * param -- 需要传给apk的参数
- */
- private void startApp(ComponentName com, String param) {
- if (com != null) {
- PackageInfo packageInfo;
- try {
- packageInfo = getPackageManager().getPackageInfo(com.getPackageName(), 0);
- } catch (NameNotFoundException e) {
- packageInfo = null;
- Toast.makeText(this, "没有安装", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- try {
- Intent intent = new Intent();
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setComponent(com);
- if (param != null) {
- Bundle bundle = new Bundle(); // 创建Bundle对象
- bundle.putString("flag", param); // 装入数据
- intent.putExtras(bundle); // 把Bundle塞入Intent里面
- }
- startActivity(intent);
- } catch (Exception e) {
- Toast.makeText(this, "启动异常", Toast.LENGTH_SHORT).show();
- }
- }
- }
- /*
- * 启动一个app
- */
- public void startAPP(String appPackageName){
- try{
- Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
- startActivity(intent);
- }catch(Exception e){
- Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
- }
- }
最后更新:2017-04-03 12:54:02