前不久到一家公司面试 职位是Android开发工程师 几道有关android的笔试题  贴出来大家讨论讨论:1、什么是ANR 如何避免它?2、什么情况会导致Force Close ?如何避免?能否捕获导致其的异常?3、Android本身的api并未声明会抛出异常,则其在运行时有无可能抛出runtime异常,你遇到过吗?诺有的话会导致什么问题?如何解决?4、简要解释一下activity、 intent  、intent filter、service、Broadcase、BroadcaseReceiver5、IntentService有何优点?
俺之前根本没接触过Android    结果可想而知

解决方案 »

  1.   

    1.在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框。
    运行在主线程里的任何方法都尽可能少做事情。特别是,Activity应该在它的关键生命周期方法(如onCreate()和onResume())里尽可能少的去做创建操作。潜在的耗时操作,例如网络或数据库操作,或者高耗时的计算如改变位图尺寸,应该在子线程里(或者以数据库操作为例,通过异步请求的方式)来完成。
    http://www.cnblogs.com/xirihanlin/archive/2010/01/07/1641621.html
      

  2.   

    5.IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate. All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.android.app.IntentService
    http://log4think.com/avoid_anr_in_android/
      

  3.   

    什么是ANR 如何避免它?
    ANR:Application Not Responding,五秒
    在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应。当出现下列情况时,Android就会显示ANR对话框了: 
    对输入事件(如按键、触摸屏事件)的响应超过5秒 
    意向接受器(intentReceiver)超过10秒钟仍未执行完毕 
    Android应用程序完全运行在一个独立的线程中(例如main)。这就意味着,任何在主线程中运行的,需要消耗大量时间的操作都会引发ANR。因为此时,你的应用程序已经没有机会去响应输入事件和意向广播(Intent broadcast)。 
    因此,任何运行在主线程中的方法,都要尽可能的只做少量的工作。特别是活动生命周期中的重要方法如onCreate()和 onResume()等更应如此。潜在的比较耗时的操作,如访问网络和数据库;或者是开销很大的计算,比如改变位图的大小,需要在一个单独的子线程中完成(或者是使用异步请求,如数据库操作)。但这并不意味着你的主线程需要进入阻塞状态已等待子线程结束 -- 也不需要调用Therad.wait()或者Thread.sleep()方法。取而代之的是,主线程为子线程提供一个句柄(Handler),让子线程在即将结束的时候调用它(xing:可以参看Snake的例子,这种方法与以前我们所接触的有所不同)。使用这种方法涉及你的应用程序,能够保证你的程序对输入保持良好的响应,从而避免因为输入事件超过5秒钟不被处理而产生的ANR。这种实践需要应用到所有显示用户界面的线程,因为他们都面临着同样的超时问题。 
    2.什么情况会导致Force Close ?如何避免?能否捕获导致其的异常?
    一般像空指针啊,可以看起logcat,然后对应到程序中 来解决错误
      

  4.   

    4、简要解释一下activity、 intent 、intent filter、service、Broadcase、BroadcaseReceiver
    一个activity呈现了一个用户可以操作的可视化用户界面
    一个service不包含可见的用户界面,而是在后台无限地运行
    可以连接到一个正在运行的服务中,连接后,可以通过服务中暴露出来的借口与其进行通信
    一个broadcast receiver是一个接收广播消息并作出回应的component,broadcast receiver没有界面
    intent:content provider在接收到ContentResolver的请求时被激活。
    activity, service和broadcast receiver是被称为intents的异步消息激活的。
    一个intent是一个Intent对象,它保存了消息的内容。对于activity和service来说,它指定了请求的操作名称和待操作数据的URI
    Intent对象可以显式的指定一个目标component。如果这样的话,android会找到这个component(基于manifest文件中的声明)并激活它。但如果一个目标不是显式指定的,android必须找到响应intent的最佳component。
    它是通过将Intent对象和目标的intent filter相比较来完成这一工作的。一个component的intent filter告诉android该component能处理的intent。intent filter也是在manifest文件中声明的。
      

  5.   

    5、IntentService有何优点?
    其实它也是避免ANR的方法:
    IntentService 的好处    * Acitivity的进程,当处理Intent的时候,会产生一个对应的Service
        * Android的进程处理器现在会尽可能的不kill掉你
        * 非常容易使用
      

  6.   

    我都不知道ANR是
    Application Not Responding
    的意思...IntentService
    Service + Handler + HandlerThread
      

  7.   

    出题的人 很蛋疼,“ANR” 不说全称,显摆自己会几个缩写么?
      

  8.   

    ANR  雅诗兰黛特润修护精华
      

  9.   

      刚开始我也这么认为   haha
      

  10.   

    这些问题都太简单了,出题人没水平,haohao nvli 
     
      

  11.   

    ANR怎么解决?正在被这个问题困扰有待具体实践还