Android的异步任务在每次启动 [英] Android Async Task on every Startup

查看:152
本文介绍了Android的异步任务在每次启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序需要运行每次启动时每次我打开一个活动,但每次应用程序启动时的异步任务......不是。我不认为该活动的生命周期将帮助我在这里,所以我在寻找的建议。

I have an android app that needs to run an async task every time it starts up... Not every time I open an activity but every time the app starts up. I don't think the activity life-cycle will help me here so I'm looking for suggestions.

推荐答案

请的应用程序,覆盖其的onCreate(),然后告诉Android的通过设置它的完整类名作为值来使用你的子类'的android:名称'的<应用/> 在你的Andr​​oid清单

Make a subclass of Application, override its onCreate() and then tell Android to use your subclass by setting its full classname as the value of 'android:name' in <application /> in your Android manifest.

<application
    android:name="com.yourpackage.YourApplicationSubclass"
    ....
/>

现在更多的细节:

android.app.Application 是基类,再presents您的应用程序。虽然你一般不需要子类应用程序,这样做给你机会让你的钩到应用程序的生命周期,而不仅仅是你的活动,意图和服务。

android.app.Application is the base class that represents your application. While you generally don't need to subclass Application, doing so gives you the opportunity to get your hooks into your application's lifecycle, rather than just your Activities', Intents', and Services'.

所有你需要做的是创建应用程序的一个新的子类:

All you need to do is create a new subclass of Application:

import android.app.Application;

public class MyApplication extends Application
{
    @Override
    public void onCreate() {
        // Do stuff when the application starts
    }
}

正如我上面所描述

然后更新您的Andr​​oidManifest.xml。

And then updated your AndroidManifest.xml as I described above.

这篇关于Android的异步任务在每次启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆