【问题标题】:How to launch other Android App from Android App?如何从 Android App 启动其他 Android App?
【发布时间】:2016-01-21 10:52:40
【问题描述】:

如果我有App的包名,如何从Android App启动其他Android App?

例如:我在 Android 应用 中创建了一个Button。当按下 Button 时,Skype 将启动。

  1. 在Android中可以做上述动作吗?
  2. Button被点击时是否应该使用Intent来启动其他App?
  3. 需要什么权限?

提前致谢。

【问题讨论】:

标签: android android-intent


【解决方案1】:

如果您有其他应用程序包名称则可以

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("other app package name");
startActivity(LaunchIntent);

【讨论】:

    【解决方案2】:

    因为它不是你的应用程序,正如你提到的,“Skype”。您可以在 Intent 中使用应用的包 ID。

    Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
    startActivity(launchIntent);
    

    对于 Skype,它变成了,

    Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.skype.raider");
    startActivity(launchIntent);
    

    在您的 Java 文件中,说 MainActivity.java

        Button button = (Button) findViewById(R.id.button);
    
        button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
    
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.skype.raider");
        startActivity(launchIntent);
                    }
                });
    

    在布局文件中,说 activity_main.xml

     <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Skype →"
            android:id="@+id/button"
            android:textColor="@color/white"/>
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码启动 Skype 或任何其他应用程序:

      PackageManager packageManager = getPackageManager();
      Intent intent = packageManager.getLaunchIntentForPackage("<skype_package_name>");
      startActivity(intent);
      

      【讨论】:

        【解决方案4】:

        试试这个代码:

        PackageManager pm = context.getPackageManager();
        try {
            if (pm.getApplicationInfo("com.your.package.name", 0) == null) {
                // no talk, no update
                Toast.makeText(context, "packagenot found", Toast.LENGTH_SHORT).show();
        
            } else {
                Intent packageIntent= pm.getLaunchIntentForPackage("com.your.package.name");
        
                packageIntent.addCategory(Intent.ACTION_SENDTO);
                packageIntent.setType("text/plain");
                startActivity(packageIntent);
            }
        } catch (PackageManager.NameNotFoundException e) {
        
            // no talk, no update
            Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
        }
        

        【讨论】:

          【解决方案5】:
              final String appPackageName = "com.example";
                                  final Intent openPlayStore = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
                                  if (hasHandlerForIntent(openPlayStore))
                                      startActivity(openPlayStore);
                                  else
                                      startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
          
          private boolean hasHandlerForIntent(Intent intent) {
                  return getActivity().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-19
            • 2013-01-03
            • 1970-01-01
            • 2018-01-13
            相关资源
            最近更新 更多