`

android培训文档提纲(四)

阅读更多
1、生命周期方法onSaveInstanceState()是在什么时候调用?是当一个Activity变成“有可能被销毁”的状态的时候被android框架调用的,包括:
1.1、用户按下HOME键
1.2、长按HOME键,选择其他程序
1.3、按下电源按键
1.4、在Activity中开启一个新的Activity时
1.5、横竖屏翻转时

但是,如果是用户按下BACK键,则当前的Activity会被销毁,并从back stack中移除,这种情况下,onSaveInstanceState()方法是不会被调用的

2、至于onRestoreInstanceState()方法,需要注意的是,onSaveInstanceState()方法和onRestoreInstanceState()方法不一定是成对的被调用的,onRestoreInstanceState()被调用的前提是,activity“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity,这种情况下activity一般不会因为内存的原因被系统销毁,故activity的onRestoreInstanceState()方法不会被执行

另外,onRestoreInstanceState()的bundle参数也会传递到onCreate方法中,因此也可以选择在onCreate()方法中做数据还原

3、The Android operating system is a multi-user Linux system in which each application is a different user.

4、By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them

5、Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications

6、By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications

7、When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component. For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process

8、A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities
开启的Service,是跑在主线程里的,也就是UI线程。所以确实是会阻塞页面刷新的,如果操作很耗时的话,甚至会引发ANR。所以如果是一个很费时的操作,需要手工地开启新线程。但是,如果不要求Service组件并发地处理多线程请求的话,可以用IntentService。这样的话,就不需要操心线程的事,一个附带的好处是,也不需要手动地stop这个Service。带来的坏处是,如果有多个Intent请求到这个Service,只能排队

9、The system calls onStartCommand() when another component, such as an activity, requests that the service be started, by calling startService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService()
Service一旦经过startService()方法调用运行起来,就会在后台默默运行,不会停止。所以如果开启了一个服务,记得通过stopService()或者stopSelf()方法,把服务关掉。同样的,如果这个服务是继承自IntentService,可以减少这个步骤

10、The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground (discussed later), then it will almost never be killed. Otherwise, if the service was started and is long-running, then the system will lower its position in the list of background tasks over time and the service will become highly susceptible to killing—if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again (though this also depends on the value you return from onStartCommand(), as discussed later)
系统如果出于回收资源的目的,把服务给停掉了,那么当资源重新可用的时候,系统会重新启动这个服务

11、A services runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service

12、The IntentService does the following:

Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
Stops the service after all start requests have been handled, so you never have to call stopSelf().
Provides default implementation of onBind() that returns null.
Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

如果不需要考虑处理并发请求,让Service继承自IntentService是一个很好的选择

13、That's all you need: a constructor and an implementation of onHandleIntent()
构造方法中的String是为了个新线程命名,通常是这样的:
public MyService(){
    super("MyServiceThread");
}

14、As you saw in the previous section, using IntentService makes your implementation of a started service very simple. If, however, you require your service to perform multi-threading (instead of processing start requests through a work queue), then you can extend the Service class to handle each intent

15、Notice that the onStartCommand() method must return an integer. The integer is a value that describes how the system should continue the service in the event that the system kills it (as discussed above, the default implementation for IntentService handles this for you, though you are able to modify it). The return value from onStartCommand() must be one of the following constants:

START_NOT_STICKY
If the system kills the service after onStartCommand() returns, do not recreate the service, unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.
START_STICKY
If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent, unless there were pending intents to start the service, in which case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job.
START_REDELIVER_INTENT
If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file.

16、The startService() method returns immediately and the Android system calls the service's onStartCommand() method. If the service is not already running, the system first calls onCreate(), then calls onStartCommand()

17、A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground

18、Service的生命周期方法包括onCreate() onStartCommand() onDestroy()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics