【问题标题】:Sending the user to another application in android将用户发送到android中的另一个应用程序
【发布时间】:2012-09-27 04:49:08
【问题描述】:

我是 android 新手,正在尝试制作一个应用程序,当用户按下应用程序 A 中的特定按钮时,他会被发送到应用程序 B。然后用户可以通过按下应用程序中的另一个按钮返回应用程序 A B. 没有内容从一个应用程序转移到另一个应用程序。

我想通过为这两个应用程序创建自定义意图来实现这一点。我应该如何开始?还有Broadcastreceiver到底是什么,我需要用它来解决上述问题吗?

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    在另一个应用程序之间切换可以通过两种方式

    1.) 如果您知道要调用的应用程序的 MainActivity,则使用

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(
                                    "package_name","package_name.MainActivity"));
        startActivity(intent);
    

    2.) 如果您不知道要调用的 MainActivity,您只需使用 PackageName,您可以使用

        Intent LaunchIntent = getPackageManager()
                                      .getLaunchIntentForPackage("package_name");
        startActivity(LaunchIntent);
    

    我认为您在这里不需要 BroadCastReceiver,因为当您想要捕获某些事件/动作以例如低电量时使用它。更多详情请查看我的回答here

    【讨论】:

      【解决方案2】:

      Code to launch external app explicitly(尤其是this answer)。您必须为每个应用程序创建一个自定义意图,然后显式调用该意图。

      在应用程序清单中:

      <intent-filter>
          <action android:name="com.mycompany.APP_A" />
      </intent-filter>
      

      在应用 B 清单中:

      <intent-filter>
          <action android:name="com.mycompany.APP_B" />
      </intent-filter>
      

      在应用程序中按下按钮:

      Intent intent = new Intent();
      intent.setAction("com.mycompany.APP_B");
      startActivity(intent);
      

      在 App B 按钮按下:

      Intent intent = new Intent();
      intent.setAction("com.mycompany.APP_A");
      startActivity(intent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多