iOS中的NSTimer 和 Android 中的Timer

首先看iOS的,

Scheduling Timers in Run Loops

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.



总结以下,在ios中,一个timer是和一个runloop密切相关的,用timer,就必须设定它的runloop,不然timer是无法正常工作的。另外一个timer invalid之后,就无法再次启用,必须新建timer。

在iOS中,如不采用特殊设置,在应用程序进入后台后,与应用程序相关的线程立即暂停,自然在thread中执行的timer也会停止运行。当应用程序再次进入前台时,暂停的thread会被恢复。

另外需要注意,看下代码

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0), ^{
        NSLog(@"async....");
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];

        timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:nil repeats:YES];

        [runloop addTimer:timer forMode:NSRunLoopCommonModes];

        [runloop run];

    });

新线程如果想启动runloop,不能单单写[runloop run],必须加入一个触发源,比如这里的timer,不然runloop是不能启动的。



在Android中,

Timer 在使用时自动开启新线程,比如以下代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

     System.out.println("Thread.currentThread()....."+Thread.currentThread().getId())  ;

        TimerTask task = new TimerTask() {
            public void run() {
                //每次需要执行的代码放到这里面。
                System.out.println("Thread.currentThread()"+Thread.currentThread().getId())  ;
                System.out.println(".........TimerTask.........run");

            }
        };

        Timer timer = new Timer();
        timer.schedule(task,1000,1000);
}

以下是输出

Thread.currentThread().....1
Thread.currentThread()319
Thread.currentThread()319

从这里我们可以看出,Android中的timer是自动创建新线程并运行的,主线程的阻塞不会影响定时器的运行。

当程序进入后台后,程序的所有线程都不会停止,直到该线程被系统或代码停止。

时间: 2024-08-11 18:01:53

iOS中的NSTimer 和 Android 中的Timer的相关文章

Android开发中遇到的问题——Android中WARNING: Application does not specify an API level requirement!的解决方法

今天在手机上调试运行Andorid项目时,发现Console打印出"WARNING: Application does not specify an API level requirement!"这样的警告信息,如下图所示: 虽然不影响项目的正常运行,不过还是要找出原因,上网查了一下出现警告的原因,原来是创建项目时AndroidManifest.xml文件中没有指定Min SDK Version 解决办法:修改AndroidManifest.xml文件,在<manifest>

Android基础入门教程——8.1.1 Android中的13种Drawable小结 Part 1

Android基础入门教程--8.1.1 Android中的13种Drawable小结 Part 1 标签(空格分隔): Android基础入门教程 本节引言: 从本节开始我们来学习Android中绘图与动画中的一些基础知识,为我们进阶部分的自定义 打下基础!而第一节我们来扣下Android中的Drawable!Android中给我们提供了多达13种的 Drawable,本节我们就来一个个撸一遍! Drawable资源使用注意事项 Drawable分为两种: 一种是我们普通的图片资源,在Andr

Android中如何修改编译的资源ID值(默认值是0x7F...可以随意改成0x02~0x7E)

一.技术准备 今天我们来看一下如何修改Android中编译时的资源Id的值,在讲解这内容之前,我们需要先了解一下Android中的资源编译之后的结构和编译过程,这里就不多说了,具体可以查看这篇文章: http://blog.csdn.net/jiangwei0910410003/article/details/50628894 这篇文章中,介绍了如何解析Android中编译之后的resource.arsc文件,这里就介绍了Android中资源文件编译之后的类型和格式,其实Android中资源编译

如何调试分析Android中发生的tombstone

如何调试分析Android中发生的tombstone Android中较容易出现以下三类问题:Force close / ANR / Tombstone 前两者主要是查看当前的进程或者系统框架层的状态和堆栈就基本可以分析出来,本文主要讨论一下tombstone的情况. tombstone一般是由Dalvik错误.状态监视调试器.C层代码以及libc的一些问题导致的. 当系统发生tombstone的时候,kernel首先会上报一个严重的警告信号(signal),上层接收到之后,进程的调试工具会把进

android中ViewHolder通用简洁写法

public class ViewHolder {     // I added a generic return type to reduce the casting noise in client code     @SuppressWarnings("unchecked")     public static <T extends View> T get(View view, int id) {         SparseArray<View> view

android 中打 Log 的一些技巧

在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋友,推荐看 android_Tutor 的博文http://blog.csdn.net/Android_Tutor/article/details/5081713 , 上面讲的很详细) 本文主要讲一下如何统一控制 Log 的输出和关闭. 一般我们会在 debug 的版本中输出 log,而在 rele

深入理解Android(2)——理解Android中的JNI(中)

阳光小强参加了CSDN博客之星评选,如果你觉得阳光小强的博客对你有所帮助,为小强投上一票吧:http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content 在上一篇中我们了解了Android中有关JNI的使用,其实JNI是很早就有的,不是在Android创造的新技术,是SUN为我们提供的一种Java和本地代码之间相互调用的方法,这一篇我们来建立一个普通的Java工程来具体看一下Java中如何调用C/C++代码. 一

彻底明白Android中AIDL及其使用

1.为什么要有AIDL? 无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在肯定合理,但是你还是没有明白.对于AIDL有一些人的浅显概念就是,AIDL可以跨进程访问其他应用程序,和其他应用程序通讯,那我告诉你,很多技术都可以访问,如广播(应用A在AndroidManifest.xml中注册指定Action的广播)应用B发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完成了通讯(但是这种通讯是单向的):还如ContentProvider,通过URI接口暴露

Android中Xposed框架篇---修改系统位置信息实现自身隐藏功能

一.前言 前文已经介绍了Xposed框架的基本使用规则,本文主要来介绍一个实际案例就是如何通过这个框架来修改系统的地理位置信息来实现隐藏功能,在如今社交工具的发展特别是微信,他有一个实时位置共享功能,那么对于那些不是单身狗的同学来说可能会有些蛋疼,哪天媳妇要查岗发送位置,结果你不在她期望的位置这时候就尴尬了,而且朋友圈在分享内容的时候可以选择当前位置,有的屌丝就像我一样没钱但是又想到处旅游,那么这时候咋们就可以一本正经的装个逼了. 二.定位原理 看到上面说的那么多,感觉这个功能必须要搞起来了,好