Android 相当于: void main() / Sub Main?

发布于 2024-10-19 10:28:13 字数 589 浏览 1 评论 0原文

我试图让我的 Android 应用程序在从启动器运行时,在启动活动之前运行一些代码。也就是说,我希望我的应用程序以 Sub Main 启动,而不是首先进入 Activity。

本质上,在伪中,我想做这样的事情:

void main() {
    doSomeInitializationStuff();

    startActivity(myFirstActivity);
}

根据这个问题,它看起来像Android从字面上看并没有这个概念。因此,我正在考虑创建一个不可见的 Activity 作为我的入口点,但无法弄清楚如何使 Activity 不可见。我已经尝试过这两种方法,这似乎是我的搜索中出现的唯一方法,但它们似乎实际上没有做任何事情...

this.setVisible(false); this.setTheme(android.R.style.Theme_Translucent_NoTitleBar);

I am trying to make my Android application run some code when run from the launcher, BEFORE launching into an activity. That is to say I want my app to start with a Sub Main as opposed to going into an Activity first.

Essentially, in pseudo, I want to do something like this:

void main() {
    doSomeInitializationStuff();

    startActivity(myFirstActivity);
}

According to this question, it looks like Android does not have this concept literally. So I was looking at creating an invisible Activity as my entry point, but cannot figure out how to make an activity invisible. I've tried these two methods, which seem to be the only ones coming up in my searches, but they don't seem to actually do anything...

this.setVisible(false);
this.setTheme(android.R.style.Theme_Translucent_NoTitleBar);

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

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

发布评论

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

评论(3

神爱温柔 2024-10-26 10:28:13

您可以创建一个启动活动并在那里开始所有初始化,而不是创建一个不可见的活动。

我还没有尝试过这个,但您可以扩展应用程序类并在应用程序类中使用 onCreate 来初始化您需要的内容。

这是应用程序类的 onCreate 的 JavaDoc

/**
 * Called when the application is starting, before any other application
 * objects have been created.  Implementations should be as quick as
 * possible (for example using lazy initialization of state) since the time
 * spent in this function directly impacts the performance of starting the
 * first activity, service, or receiver in a process.
 * If you override this method, be sure to call super.onCreate().
 */
public void onCreate() {
}

您需要通过使用 Android 清单文件标记中的 android:name 参数让应用程序知道您正在使用自定义应用程序类。

Instead of creating an invisible activity you can create a splash activity and start all your initializations there .

I have not tried this but you can extend the application class and use onCreate in your application class to initialize what you need.

Here is the JavaDoc for onCreate of the application class

/**
 * Called when the application is starting, before any other application
 * objects have been created.  Implementations should be as quick as
 * possible (for example using lazy initialization of state) since the time
 * spent in this function directly impacts the performance of starting the
 * first activity, service, or receiver in a process.
 * If you override this method, be sure to call super.onCreate().
 */
public void onCreate() {
}

You will need to let the app know that you are using a custom application class by using the the android:name parameter in the tag of the Android manifest file.

我一直都在从未离去 2024-10-26 10:28:13

在大多数 Android 应用程序中,都有 SplashScreen 的概念,人们可以使用该屏幕来完成此类行为,而该 SplashScreen 的真正主题是在后台处理此类任务,同时为应用程序本身以及与之相关的各种事物做广告。

In most of the Android application there is concept of SplashScreen one can use that screen to accomplish such behaviour and the real motif is of this SplashScreen is to proccess such tasks in background while advertising for the app itself and various things related to that

独留℉清风醉 2024-10-26 10:28:13

一种选择是不使用不可见的 Activity,而是使用 SplashScreen。这样做的优点是,用户在应用程序启动时就已经看到发生了一些事情,这样他就不会产生应用程序不起作用的印象。有关示例,请参见此类 ;您可以将 doSomeInitStuff() 放在第 54 行左右,

否则我认为,您不能在第一个活动的 onCreate() 中加载布局,然后从那里转发。

One option would be to not have an invisible Activity, but a SplashScreen. This has the advantage that the user already sees something happening when the app starts up, so that he does not get the impression it is no working. For an example see e.g. this class; you would put the doSomeInitStuff() at around line 54

Otherwise I think, you can just not load a layout in onCreate() of the first activity and then forward from there.

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