7

我的Android 4+应用程序已连接到用于每隔几分钟同步一次数据的自定义Web服务。为了确保在线数据始终处于最新状态,我希望在应用程序关闭/发送到后台时触发同步。如何在Android应用程序关闭/设置为背景时执行后台任务?

在iOS版,这是很容易的:

  • applicationDidEnterBackground:在AppDelegate中
  • 使用beginBackgroundTaskWithName:开始您长时间运行的后台任务,并避免在任务仍在运行
被暂停

如何做到这一点在Android?

第一个问题是,没有什么等于applicationDidEnterBackground:。我迄今发现的所有解决方案均建议使用主要活动onPause()方法。然而,这被称为任何时候主Activity暂停,这也是在应用程序内启动另一个Activity时的情况。从ActivityLifecycleCallbacks接口的onActivityPaused()方法是正确的。

那么,如何检测到应用程序(而不仅仅是一个Activity)已关闭或发送到后台?

第二个问题是,我没有找到任何有关如何运行后台任务的信息。所有解决方案都指向简单地开始AsyncTask,但这真的是一个正确的解决方案吗?

AsyncTask将开始一个新的任务,但这个任务也运行,当应用程序被关闭/无效?我需要做些什么来防止应用程序和任务在几秒钟后被暂停?在应用程序完全暂停之前,如何确定该任务可以完成?

+0

您可以使用该服务 –

+0

您是否考虑过服务? – apmartin1991

+0

使用服务https://developer.android.com/reference/android/app/Service.html –

回答

8

下面的代码将有助于当你的应用程序被关闭或在后台您来发表您的内容:

import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.Handler; 
import android.os.IBinder; 
import android.widget.Toast; 

public class SendDataService extends Service { 
    private final LocalBinder mBinder = new LocalBinder(); 
    protected Handler handler; 
    protected Toast mToast; 

    public class LocalBinder extends Binder { 
     public SendDataService getService() { 
      return SendDataService .this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     handler = new Handler(); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 

// write your code to post content on server 
      } 
     }); 
     return android.app.Service.START_STICKY; 
    } 

} 

我的代码更多的解释是:

服务中运行,即使您的应用程序在后台背景,但请记住它始终运行在主线程,因为其中的我创建了一个单独的线程来执行你的后台操作:

服务,如果因为任何原因,你的服务中得到背景和破坏作为甚至开始作为一个粘它会自动重新启动。

更多细节可以在这里找到: https://developer.android.com/guide/components/services.html

,并检查您的应用是否在前台/后台下面的代码将有助于:

private boolean isAppOnForeground(Context context) { 
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); 
    if (appProcesses == null) { 
     return false; 
    } 
    final String packageName = context.getPackageName(); 
    for (RunningAppProcessInfo appProcess : appProcesses) { 
     if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { 
     return true; 
     } 
    } 
    return false; 
    } 
} 

// Use like this: 
boolean foregroud = new ForegroundCheckTask().execute(context).get(); 

编码愉快!!!!

+0

Downvoted,因为只是在没有解释的情况下显示代码,并期望原始海报自动理解它,并不总是如何发生。如果你改善你的答案,将会删除我的downvote。 – Diti

+0

@Diti快乐编码! :D – Anjali

+0

完成!感谢您花了一点时间让初学者更容易理解答案! – Diti

3

你可以为你想达到什么使用的服务。如果您尚未调用stopService(..)或stopSelf()方法,即使活动组件已被销毁,服务仍将继续在后台运行

在服务中,您可以进行异步网络调用,最好使用翻新,然后您可以使用从Web服务中获取的最新数据更新本地存储,如Sqlite数据库。

Here是官方的链接,你可以使用一个无界的服务来实现你想要的。

+0

非常感谢。 “服务”似乎确实是问题编号的完美解决方案。 2.第一个问题:如何正确检测到应用程序已暂停/关闭,而不仅仅是一个活动? –

+0

为此,您可以重写onPause()和onDestroy()等活动生命周期方法。如果您的活动是后退堆栈中的最后一项,并按下了默认的后退按钮,则onDestroy()会被调用,并退出应用程序。 –

相关问题