Android 相当于: void main() / Sub Main?
我试图让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个启动活动并在那里开始所有初始化,而不是创建一个不可见的活动。
我还没有尝试过这个,但您可以扩展应用程序类并在应用程序类中使用 onCreate 来初始化您需要的内容。
这是应用程序类的 onCreate 的 JavaDoc
您需要通过使用 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
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.
在大多数 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
一种选择是不使用不可见的 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.