在不知道应用程序名称的情况下启动当前应用程序的 MAIN 活动

发布于 2024-11-26 14:26:26 字数 503 浏览 1 评论 0原文

我正在尝试编写一个实用程序方法,该方法能够启动标记为“android.intent.action.MAIN”的活动(属于当前应用程序)。实用程序方法不应接受任何参数。

所需的代码:

public void startMainActivity(Context context) {
    ...
}

清单:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

有什么想法吗?

I am trying to write an utility method, that would be able to start activity (belonging to current application) marked as "android.intent.action.MAIN". The utility method should not accept any parameters.

Desired code:

public void startMainActivity(Context context) {
    ...
}

Manifest:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶色山野 2024-12-03 14:26:26

从 API 级别 3 (Android 1.5) 开始,此功能有效:

private void startMainActivity(Context context) throws NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
    context.startActivity(intent);
}

This works since API Level 3 (Android 1.5):

private void startMainActivity(Context context) throws NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
    context.startActivity(intent);
}
浅暮の光 2024-12-03 14:26:26

我们使用了 alex2k8 这个很好的解决方案一段时间,直到发现它不能所有设备上运行,并且从 Google Play 下载的发布版本

不幸的是系统没有:

  • 抛出任何异常
  • 记录错误的原因

我们使用以下解决方法来解决它:

protected void startMainActivityWithWorkaround() throws NameNotFoundException, ActivityNotFoundException {
    final String packageName = getPackageName();
    final Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
    if (launchIntent == null) {
      Log.e(LOG_TAG, "Launch intent is null");
    } else {
      final String mainActivity = launchIntent.getComponent().getClassName();
      Log.d(LOG_TAG, String.format("Open activity with package name %s / class name %s", packageName, mainActivity));
      final Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      intent.setComponent(new ComponentName(packageName, mainActivity));
      // optional: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
    }
}

We used the nice solution of alex2k8 for a while until discovering it was not working on all devices on released version downloaded from Google Play.

Unfortunately the system didn't:

  • throw any exception
  • log the cause of the error

We used following workaround to solve it:

protected void startMainActivityWithWorkaround() throws NameNotFoundException, ActivityNotFoundException {
    final String packageName = getPackageName();
    final Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
    if (launchIntent == null) {
      Log.e(LOG_TAG, "Launch intent is null");
    } else {
      final String mainActivity = launchIntent.getComponent().getClassName();
      Log.d(LOG_TAG, String.format("Open activity with package name %s / class name %s", packageName, mainActivity));
      final Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      intent.setComponent(new ComponentName(packageName, mainActivity));
      // optional: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
    }
}
泅人 2024-12-03 14:26:26

我认为您必须找到清单中声明的​​所有活动的列表,然后使用intentFilter的 actionsIterator() 迭代每个活动的意图过滤器的所有操作,匹配具有intent.action.MAIN 然后启动该 Activity。

问题是我不确定如何从清单中检索所有声明的活动的列表。

I think you'd have to find a list of all the activities declared in your manifest, and then use the intentFilter's actionsIterator() to iterate through all the actions of each activity's intent-filter, match the one that has intent.action.MAIN and then start that Activity.

The problem is I'm not sure how to retrieve a list of all your declared Activites from the manifest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文