安卓的AsyncTask VS Runnable接口VS的TimerTask VS服务 [英] Android Asynctask vs Runnable vs timertask vs Service

查看:263
本文介绍了安卓的AsyncTask VS Runnable接口VS的TimerTask VS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是什么方法(类)?

What are the differences between these methods (classes)?

我要运行,运行每5秒一个应用程序,清除内存中完成时,当CPU处于待机模式,您可以运行的应用程序。因此,该应用程序没有绑定到一个wakelock。

I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a wakelock.

问候,

沙夫卡特

推荐答案

前三个之间的区别是工作刚刚已经为你做了多少。和一个 服务 fundamental Android应用程序组件

The difference between first three is just the amount of work that has been done for you. And a Service is a fundamental Android application component.

AsyncTask的 作为一个方便的类上做了一些工作一个新的线程,并使用结果完成后,从中得到了所谓的线程(通常是UI线程)上。这只是一个包装,它使用一对夫妇可运行的,但处理创建线程和线程之间处理消息的所有复杂。

AsyncTask as a convenience class for doing some work on a new thread and use the results on the thread from which it got called (usually the UI thread) when finished. It's just a wrapper which uses a couple of runnables but handles all the intricacies of creating the thread and handling messaging between the threads.

<一个href="http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runnable.html">Runnable接口是在Java线程的核心。在的Runnable 接口应该被任何类的实例,旨在由一个线程来执行。执行

The Runnable interface is at the core of Java threading. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

<一个href="http://download.oracle.com/javase/1.4.2/docs/api/java/util/TimerTask.html">TimerTask是标准的Java的一部分,可用于延迟或重复执行的一些片(可运行)code。它的用途是鼓励在Android 。您可以使用一个处理器来代替。

TimerTask is part of standard Java and can be use for delayed or repeated execution for some piece of (Runnable) code. It's use is discouraged on Android. You can use a Handler instead.

一个服务可以作为Android应用程序的独立和非用户界面的一部分。它可以运行,并创建自己的线程,可以通过 <一开始的UI或意图href="http://developer.android.com/reference/android/app/AlarmManager.html">AlarmManager例如,

A Service can be used as a independent and UI-less part of your Android application. It can run and create it's own threads and can be started for UI or with Intents through a AlarmManager for example.

它想不想你想要的是创造它自己的线程,并做了一些工作服务。当工作完成后,内存被释放在Android当垃圾收集踢,有些东西你不要管,这是一个很好的事情。

It think want you want is a Service which creates it's own thread and does some work. When the work is done, memory will be freed on Android when the garbage collector kicks in, something you do not control and that's a good thing.

在AlarmManager允许你播放意图在指定的时间间隔,甚至允许控制,以唤醒设备或没有。你只需要定义一个BroadcastReceiver在你的服务和你的清单申报。

The AlarmManager allows you to broadcast Intents at specified intervals and even allow control to wake-up the device or not. You just have to define a BroadcastReceiver in your Service and declare it in your manifest.

你的问题的最后一部分,我真的不明白,所以请澄清多一点对你要完成的任务。

The last part of your question I don't really understand, so please clarify a bit more on what your trying to accomplish.

这篇关于安卓的AsyncTask VS Runnable接口VS的TimerTask VS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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