从应用程序启动不同的启动器活动

Start different launcher-activty from MyApplication

我的代码AndroidManifest.xml 中,启动器设置为 X activity、

里面 public class MyApplication extends Application 我有:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}

我想做的事情: 在 MyApplication 中检查用户是 运行 来自 phone 的应用程序还是平板电脑,如果是平板电脑则转到 activity B,或者 activity A 如果它是 phone.

到目前为止我做了什么:

里面onCreate()

if (!isTablet(MyApplication.this)) {
    Intent intent = new Intent(MyApplication.this, SplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} else {
    Intent intent = new Intent(MyApplication.this, XlSplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

有没有办法在清单中省略启动器 Activity,让应用程序 class 决定应该启动哪个 activity?

您可以使用 drawable(-s) 文件夹显示不同的启动画面。

更多:https://developer.android.com/guide/topics/resources/providing-resources

应用程序 class 不打算调用 startActivity(intent)

您可以创建一个没有布局的新 Activity,并将其设置为启动 activity。然后,在这个 activity 的 onCreate() 方法中,检查它是平板电脑还是 phone 并启动所需的 Activity.

但是,我认为你应该避免这种方法,看看Fragments

更新

您不能让应用程序 class 决定应该启动哪个 Activity。应用程序 class 包含您的应用程序的公共部分,即使您的应用程序未显示,这些部分也应该可以正常工作。事实上,它通常用于设置通知的事件侦听器,因为即使应用程序不在屏幕上,也会实例化此 class。

另一种解决方案是制作两个不同的应用程序,一个用于 phone ,另一个用于平板电脑。然而,片段方法仍然是最好的。

由于您的 activity 是启动画面,我认为两者之间的主要区别在于 UI 配置和大小。因此,您可以做的最好的事情是 运行 相同 activity 但针对不同的屏幕分辨率和方向使用不同的布局文件或资源。这是使用平板电脑的正确方式。

您应该允许 Android 通过文件夹结构本机为您处理此问题。

res/layout-sw600dp/ # For 7” tablets (600dp wide and bigger)

res/layout-sw720dp/ # For 10” tablets (720dp wide and bigger)

res/layout-sw600dp-port/ # For 7” tablets in portrait (600dp wide or bigger)

res/layout-sw720dp-port/ # For 10” tablets in portrait (720dp wide or bigger)

res/layout-sw600dp-land/ # For 7” tablets in portrait (600dp wide or bigger)

res/layout-sw720dp-land/ # For 10” tablets in portrait (720dp wide or bigger)

因此,如果您正在设计带有一些风景变体的肖像,那么您可以制作一个 -land 文件夹。

如果您正在设计具有一些纵向变体的横向,那么您可以制作一个 -port 文件夹。

或者您可以简单地回复 size 文件夹,如果设计对两者都适用,则不要创建任何 -land 或 -port 文件夹。