【问题标题】:How do I programmatically launch a specific application?如何以编程方式启动特定应用程序?
【发布时间】:2011-03-21 13:58:31
【问题描述】:

我想启动一个特定的应用程序。

我知道如何执行 Intent,但如果有多个可以处理 Intent 的应用程序,我想避免选择菜单,我想直接转到特定应用程序。希望这是有道理的。

【问题讨论】:

标签: android


【解决方案1】:

您直接使用包名/类,例如创建一个新意图来调用您将使用以下link text 的 twidroid 程序:

 Intent intent = new Intent("com.twidroid.SendTweet");

您可能希望在未安装应用程序时尝试/捕获 ActivityNotFoundException。

【讨论】:

  • 如果发生这种情况,您可以抛出异常并在 GooglePlay 上打开应用程序。 ;)
【解决方案2】:
Intent intent = new Intent();    
intent.setClassName("package.name", "package.name.LauncherActivityName");
startActivityForResult(intent,REQUEST_CODE);

【讨论】:

    【解决方案3】:

    您正在调用的活动不仅应该出现在它自己的包的清单中,还应该出现在 CALLING 包的清单中。 - 不要忘记!

    【讨论】:

      【解决方案4】:

      你应该使用包管理器的功能。

      Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**
      try {
      Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
      ctx.startActivity(i);
      } catch (NameNotFoundException e) {
          // TODO Auto-generated catch block
      }
      

      【讨论】:

      • 什么是 ctx ?对象还是关键字?
      • 这是一个Android Context object,例如一个 Activity 或 getApplicationContext() 的返回值。
      • 重要的是:只有在 Activity Class 中才能使用 getApplicationContext。
      • 不完全正确。它适用于 ContextWrapper 的所有后代。其他重要示例是 Application 类和 Service 类。无论如何,在视图上你可以使用 getContext(),在 Fragments 中你可以使用 getActivity()。通常获取一个有效的 Context 对象是没有问题的......这就是为什么我没有在我的原始答案中明确定义它。
      • 所有场景的最佳答案
      【解决方案5】:

      我使用:

        try {
             Intent intent = new Intent();    
             intent.setClassName("package.name", "<your_package_name>");
             startActivity(intent);
          } catch (NameNotFoundException e) {
             Log.e(TAG, e.getMessage());
          }
      

      但就像 Cami 建议的那样,这也可以:

      try {
          Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
          ctx.startActivity(i);
      } catch (NameNotFoundException e) {
           Log.e(TAG, e.getMessage());
      }
      

      【讨论】:

        【解决方案6】:

        oncreate方法调用=> openApp();方法

        private void openApp() {
            String packageName = "com.google.android.gm";
            if (isAppInstalled(activity, packageName))
                startActivity(getPackageManager().getLaunchIntentForPackage(packageName));
            else Toast.makeText(activity, "App not installed", Toast.LENGTH_SHORT).show();
        }
        
        public static boolean isAppInstalled(Activity activity, String packageName) {
            PackageManager pm = activity.getPackageManager();
            try {
                pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
            }
            return false;
        }
        

        【讨论】:

          【解决方案7】:

          假设您想从您的应用中打开 Wonder Clock

          网址:https://play.google.com/store/apps/details?id=ganesha.diva.app.wonderclock.free&hl=en_US

          id 字段/包名是我们感兴趣的 = ganesha.diva.app.wonderclock.free

          包管理器跟踪设备上安装的应用程序并维护包列表。因此,任何与设备上已安装应用程序相关的内容,都应咨询包管理器。

          bundle id 或包名称在全球范围内是唯一的,因此可用于检查设备上的包/应用程序是否存在。

          从您的应用打开Wonder Clock

          • 向包管理器询问给定包名称的启动器活动

            Intent intent = context.getPackageManager().getLaunchIntentForPackage("ganesha.diva.app.wonderclock.free");

          • 检查启动器活动是否存在 if(intent != null) continue... 否则设备上未安装应用程序将用户带到 URL

          • 将所需的标志添加到意图 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

          • 开始活动 startActivity(intent);

          【讨论】:

            【解决方案8】:

            我已经解决了问题

            String packageName = "Your package name";
            
            Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
            
            if(intent == null) {
                try {
                    // if play store installed, open play store, else open browser 
                     intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
                } catch (Exception e) {
                    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
                }
            } 
            startActivity(intent);
            

            【讨论】:

              【解决方案9】:

              使用IntentComponentName 启动应用

              ComponentName cName = new ComponentName("packageName","packagename.yourMainActivity");
              Intent intent = new Intent("android.intent.action.MAIN");
              intent.setComponent(cName);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              mContext.startActivity(intent);
              

              【讨论】:

                【解决方案10】:

                我打开应用程序是这样做的

                private void _LaunchApp(final String _Pack) {
                    Intent launchi = new Intent(Intent.ACTION_VIEW);
                    launchi.setData(Uri.parse("android-app://".concat(_Pack)));
                    startActivity(launchi);
                }
                

                及用途

                _LaunchApp("your.package.here");
                

                【讨论】:

                • 请遵循 Java 命名约定。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-03-27
                • 1970-01-01
                • 2013-02-05
                • 1970-01-01
                相关资源
                最近更新 更多