jason23reg

jason23reg

导航

Application Fundamentals (2.2)

process attribute: <application>, <activity>, <service>, <provider>, <receiver>

android.app.Activity  <activity>
    void Context.startActivity(Intent intent)
    void Activity.startActivityForResult(Intent intent, int requestCode)
        void Activity.onActivityResult(int requestCode, int resultCode, Intent data)
    Intent Activity.getIntent()
    Activity.onNewIntent(Intent intent)
    void Activity.finish()
    void Activity.finishActivity(int requestCode)

android.app.Service  <service>
    ComponentName Context.startService(Intent service)
        int Service.onStartCommand(Intent intent, int flags, int startId)
    boolean Context.bindService(Intent service, ServiceConnection conn, int flags)
        IBinder Service.onBind(Intent intent)
    void Context.unbindService(ServiceConnection conn)
    boolean Context.stopService(Intent service); void Service.stopSelf(); boolean stopSelfResult(int startId)

android.content.BroadcastReceiver [ <receiver> ]
    void Context.sendBroadcast(Intent intent)
    void Context.sendOrderedBroadcast(Intent intent, String receiverPermission)
    void Context.sendStickyBroadcast(Intent intent)
        void BroadcastReceiver.onReceive(Context context, Intent  intent)
    Intent Context.registerReceiver(BroadcastReceiver receiver, IntentFilter filter)

android.content.ContentProvider <provider>  <==  android.content.ContentResolver

android.content.Intent    <intent-filter>
     android.content.IntentFilterIntent filters are declared in the manifest file.


task, stack
root activity, top activity

The stack contains objects. Activities in the stack are never rearranged, only pushed and popped.
Values for the task as a whole are set in the root activity.


Intent flags:
FLAG_ACTIVITY_NEW_TASK  *
FLAG_ACTIVITY_CLEAR_TOP  *
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP 


<activity> attributes:
taskAffinity  *
launchMode  (standard, singleTop, singleTask, singleInstance)  *
allowTaskReparenting  *
clearTaskOnLaunch  *
alwaysRetainTaskState  *
finishOnTaskLaunch  *


"singleTask" and "singleInstance" should be used only when the activity has a MAIN and LAUNCHER filter.

All components are instantiated in the main thread of the specified process. Methods that respond to those calls — methods like android.view.View.onKeyDown() - always run in the main thread of the process.

The decision whether to terminate a process, therefore, depends on the state of the components running in that process.

Threads are created in code using java.lang.Thread objects.
android.os.Looper for running a message loop within a thread.
android.os.Handler for processing messages.
android.os.HandlerThread for setting up a thread with a message loop.


=======================================================
RPC

By default, all methods are executed synchronously (the local method blocks until the remote method finishes), even if there is no return value.

aidl tool.

void  android.content.ServiceConnection.onServiceConnected(ComponentName  name, IBinder  service)
void  android.content.ServiceConnection.onServiceDisconnected(ComponentName  name)

boolean  android.content.Context.bindService(Intent  service, ServiceConnection  conn, int flags)

IBinder  android.app.Service.onBind(Intent  intent)


=======================================================
Thread-safe methods
ContentProvider methods that respond to those requests — the methods query(), insert(), delete(), update(), and getType() — are called from a pool of threads in the content provider's process, not the main thread of the process.
Since these methods may be called from any number of threads at the same time, they too must be implemented to be thread-safe. 


=======================================================
The lifecycle hook method should always first call the superclass version.

Activity lifecycle
1)active
2)paused
3)stopped

void onCreate(Bundle savedInstanceState)
    [void onRestart()]
    void onStart()
    (void Activity.onRestoreInstanceState(Bundle savedInstanceState))
        void onResume() - active

        (void Activity.onSaveInstanceState(Bundle outState))
        void onPause() - paused
    void onStop() - stopped
void onDestroy()        boolean Activity.isFinishing()

Saving activity state

onSaveInstanceState() and onRestoreInstanceState()  are not lifecycle methods. They are not always called.
For example, Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key). In that case, the user won't expect to return to the activity, so there's no reason to save its state.

void Activity.onRestoreInstanceState(Bundle savedInstanceState)
void Activity.onSaveInstanceState(Bundle outState)


Coordinating activities
1. The current activity's onPause() method is called.
2. Next, the starting activity's onCreate(), onStart(), and onResume() methods are called in sequence.
3. Then, if the starting activity is no longer visible on screen, its onStop() method is called.


=================================================================
Service lifecycle

void Service.onCreate()
void Service.onStart(Intent intent, int startId) :called only for services started by startService().
void Servie.onDestroy()

IBinder Service.onBind(Intent intent)
boolean Service.onUnbind(Intent intent)
void Service.onRebind(Intent intent)


=================================================================
Broadcast receiver lifecycle

void BroadcastReceiver.onReceive(Context curContext, Intent broadcastMsg)
    The broadcast receiver is considered to be active only while it is executing this method. When onReceive() returns, it is inactive.


=================================================================
Processes and lifecycles

The Android system tries to maintain an application process for as long as possible, but eventually it will need to remove old processes when memory runs low.
Android places each process into an "importance hierarchy" based on the components running in it and the state of those components:

1.foreground process
    1.1 It is running an activity that the user is interacting with (the Activity object's onResume() method has been called).
    1.2 It hosts a service that's bound to the activity that the user is interacting with.
    1.3 It has a Service object that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()).
    1.4 It has a BroadcastReceiver object that's executing its onReceive() method.
2.visible process
    2.1 It hosts an activity that is not in the foreground, but is still visible to the user (its onPause() method has been called).
    2.2 It hosts a service that's bound to a visible activity.
3.service process
    3.1 The process running a service that has been started with the startService() method and that does not fall into either of the two higher categories.
4.background process
    4.1 The process holding an activity that's not currently visible to the user (the Activity object's onStop() method has been called).
5.empty process
    5.1 The process doesn't hold any active application components. The only reason to keep such a process around is as a cache to improve startup time the next time a component needs to run in it.


A process's ranking may be increased because other processes are dependent on it. A process that is serving another process can never be ranked lower than the process it is serving.

posted on 2010-10-02 21:32  jason23reg  阅读(225)  评论(0编辑  收藏  举报