What is Android?

Androidis a software stack for mobile devices that includes an operating system,middleware and key applications. TheAndroid SDKprovides the tools and APIs necessary to begin developing applications on theAndroid platform using the Java programming language.

Android是一个包含了操作系统、中间件和关键应用的软件栈。


Activating Components

to activate a component in another application, youmust deliver a message to the system that specifies yourintentto start a particular component. The system then activates the component foryou.

3种组件类型—activities, services, and broadcast receivers—通过异步信息intent启动。Contentprovider 会在通过ContentResolver query()等时,启动。

There are separate methods foractiviting each type of component:

  • You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result).
  • You can start a service (or give new instructions to an ongoing service) by passing anIntent to startService(). Or you can bind to the service by passing an Intent to bindService().
  • You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
  • You can perform a query to a content provider by calling query() on a ContentResolver.

Activities

An Activity is an applicationcomponent that provides a screen with which users can interact in order to dosomething。 Activity是提供用户交互接口的一种应用组件。

一个应用程序通常由若干个activity组成,松耦合。并且通常会有一个main, launcher的activity,作为该应用启动时的第一个activity显示。 系统中有一个用来保存activity的“后进先出”栈,当一个新的activity启动时,该activity会被压到栈顶,并得到用户焦点;当用户关闭当前activity或者按下返回键时,当前activity会被从栈顶弹出,而之前的activity上升到栈顶位置,resume给用户。

Activity的生命周期

Method

Description

Killable after?

Next

onCreate()

Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on.

Always followed by onStart().

No

onStart()

onRestart()

Called after the activity has been stopped, just prior to it being started again.

Always followed by onStart()

No

onStart()

onStart()

Called just before the activity becomes visible to the user.

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

No

onResume()
or
onStop()

onResume()

Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

Always followed by onPause().

No

onPause()

onPause()

Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

Yes

onResume()
or
onStop()

onStop()

Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

Yes

onRestart()
or
onDestroy()

onDestroy()

Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone calledfinish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with theisFinishing() method.

Yes

nothing

Task是与用户进行交互的activity的集,这些activity被安排在一个“后进先出”的栈中。

A task is a cohesive unit that can move to the"background" when users begin a new task or go to the Home screen,via the HOME key. While in the background, all the activities in the task arestopped, but the back stack for the task remains intact—the task has simplylost focus while another task takes place, as shown in figure 2. A task canthen return to the "foreground" so users can pick up where they leftoff.

  • Activity A 启动activity B时,A被停止,但是系统仍保持其状态。若用户在B中按返回键,系统恢复A刚才的状态并将其显示出来。
  • 当用户按下HOME键,当前的activity停止,其所在的task进入后台状态,系统保持其状态。若用户稍后重新启动该程序,则该task重新进入前台,并显示栈顶的activity。
  • 当用户按返回键,当前的activity被弹出task栈,并被摧毁,此时activity的状态不会被保存。
  • Activities can be instantiated multiple times, even from other tasks.

Services

Service一般用来在后台执行一些long-running的操作。

Service 主要有2种形式:

Started

当应用组件调用startService()启动一个service,该sevice变成“started”。一旦启动,该service就会自动在后台运行,即使在启动它的组件被销毁之后。Service一般执行单一操作,且不返回结果给调用者。当操作完成,service应当自行停止。

Bound

当应用组件条用bindService()绑定一个service,该service变成“bound”。一个绑定的service提供客户-服务器的接口,组件利用这些接口来与service进行交互,或者与其他进程通信(IPC)。多个应用组件可以同时绑定到同一个service,当所有的组件都unbind后,该service停止。

不管你的应用是否启动,绑定;任何应用的组件都可以通过intent启动你的service,并使用它。当然你也可以在你的AndroidManifest里将该service声明为private来防止其他程序启动这个service。

注意:一个service运行在创建它的线程中。如果你要做比较消耗cpu的操作,应该在service里面另外创建线程,以防止出现 Application Not Responding(ANR) errors .

主要的回调函数:

onStartCommand()

当service启动时调用,当操作完成时,需要调用stopSelf()或者stopService()停止service。 (If you only want to providebinding, you don't need to implement this method.)

onBind()

当另一个组件调用bindService()时条用,需要返回Ibinder对象,client利用该对象提供的接口与service通信。 You must always implement thismethod, but if you don't want to allow binding, then you should return null.

onCreate()

The system calls this method when the service is firstcreated, to perform one-time setup procedures (before it calls eitheronStartCommand() or onBind()). If the service is alreadyrunning, this method is not called.

onDestroy()

The system calls this method when the service is no longerused and is being destroyed. Your service should implement this to clean up anyresources such as threads, registered listeners, receivers, etc. This is thelast call the service receives.

一般通过继承以下2个类来创建一个started service:

Service

This is the base class for all services. When you extendthis class, it's important that you create a new thread in which to do all the service's work,because the service uses your application's main thread, by default, whichcould slow the performance of any activity your application is running.

IntentService

This is a subclass of Service that uses a worker thread to handle all startrequests, one at a time. This is the best option if you don't require that yourservice handle multiple requests simultaneously. All you need to do isimplementonHandleIntent(),which receives the intent for each start request so you can do the backgroundwork.

TheIntentService 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 callstopSelf().
  • 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.

All this adds up to the fact thatall you need to do is implement onHandleIntent() to do the work provided by theclient. (Though, you also need to provide a small constructor for the service.)



更多相关文章

  1. 设置组件圆角
  2. Android(安卓)Jetpack
  3. Android(安卓)手势检测及通过手势实现翻页效果
  4. Android(安卓)MVC,MVP和MVVM架构模式的探究
  5. Android快速入门 四大应用组件之一Activity(打电话和发短信)功能练
  6. 【Android】Android设计准则
  7. Android开发指南-用户界面-事件处理
  8. 移动系统后起之秀渐露颓势:谷歌兄,拉Android一把(转载)
  9. Android(安卓)APP首次登录和之后自动登录流程

随机推荐

  1. Android存取txt
  2. android google directions
  3. android使用shape设置下边框
  4. android 图片处理工具类
  5. Android中使用TabHost实现类似标签栏的效
  6. android表情Gson EditText TextView
  7. Android之查看手机实时电流、电压
  8. Andrpid Activity作Dialog使用
  9. android IntentService
  10. android字符串 优化(一)