”Android Application Framework FAQ“关于Android框架的常见问题

Android Application Framework FAQ

  • Do all the Activities and Services of an application run in a single process?
  • Do all Activities run in the main thread of an application process?
  • How do I pass complicated data structures from one Activity/Service to another?
  • How can I check if an Activity is already running before starting it?
  • If an Activity starts a remote service, is there any way for the Service to pass a message back to the Activity?
  • How to avoid getting the Application not responding dialog?
  • How does an application know if a package is added or removed?

Do all the Activities and Services of an application run in a single process?

All Activities and Services in an application run in a single process by default. If needed, you can declare anandroid:process attribute in your manifest file, to explicitly place a component (Activity/Service) in another process.

Do all Activities run in the main thread of an application process?

By default, all of the application code in a single process runs in the main UI thread. This is the same thread that also handles UI events. The only exception is the code that handles IPC calls coming in from other processes. The system maintains a separate pool of transaction threads in each process to dispatch all incoming IPC calls. The developer should create separate threads for any long-running code, to avoid blocking the main UI thread.

How do I pass data between Activities/Services within a single application?

It depends on the type of data that you want to share:

Primitive Data Types

To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.

Non-Persistent Objects

For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:

Singleton class

You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value.

A public static field/method

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.

Persistent Objects

Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.

For sharing complex persistent user-defined objects, the following approaches are recommended:

  • Application Preferences
  • Files
  • contentProviders
  • SQLite DB

If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer to theData Storage for further details on how to use these components.

How can I check if an Activity is already running before starting it?

The general mechanism to start a new activity if its not running— or to bring the activity stack to the front if is already running in the background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() call.

If an Activity starts a remote service, is there any way for the Service to pass a message back to the Activity?

See the Service documentation's for examples of how clients can interact with a service. You can take advantage of the fact that your components run in the same process to greatly simplify service interaction from the generic remote case, as shown by the "Local Service Sample". In some cases techniques like singletons may also make sense.

How to avoid getting the Application not responding dialog?

Please read the Designing for Responsiveness document.

How does an application know if a package is added or removed?

Whenever a package is added, an intent with PACKAGE_ADDED action is broadcast by the system. Similarly when a package is removed, an intent with PACKAGE_REMOVED action is broadcast. To receive these intents, you should write something like this:

<receiver android:name ="com.android.samples.app.PackageReceiver">
      <intent-filter>
         <action android:name="android.intent.action.PACKAGE_ADDED"/>
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>            
            
       <data android:scheme="package" />
    </intent-filter>
 </receiver>
Here PackageReceiver is a BroadcastReceiver class.Its onReceive() method is invoked, every time an application package is installed or removed.

译文:

编号 问题
1 在一个应用当中所有的Activity和Service都在单独一个进程当中运行吗?
2 所有的acitivity都在应用主线程当中吗?
3 如何在不同的Acvitiy或者是Service当中传递复杂数据类型?
4 如何在一个Activity没有启动之前判断它是否已经在运行当中?
5 如果一个Activity启动了一个远程的服务,有没有一种方法让Service中的数据传回Activity?
6 如何避免得到应用程序没有响应的对话框?ANR:应用程序无响应
7 一个应用程序如何才能知道一个包的添加或删除?

一、在一个应用当中所有的Activity和Service都在单独一个进程当中运行吗?

在默认情况下一个应用当中所有的Acitivity和Serivce都是在一个单独的进程当中运行的。如果需要的话,你可以在配置文件(manifest file)当中声明一个android:process属性,这样就可以明确的将应用当中的一部分(Activity或者Service)放入另外的一个进程当中。

二、所有的acitivity都在应用主线程当中吗?

默认情况下,应用当中所有的在一个单独线程当中带代码都再UI主线程当中运行。这个线程同时处理UI的事件。唯一不同的地方就是那些处理来自其他进程的IPC调用的代码。系统会在每个进程当中维护一个单独的业务处理线程池来处理那些到来的IPC调用。开发者应当建立为那些需要长时间运行的代码建立另外一个线程,方式阻塞主线程。

三、如何在不同的Acvitiy或者是Service当中传递复杂数据类型?

这取决于你想分享的数据是什么类型:

基本数据类型

为了在一个应用程序当中不同的Activity或者Service之间共享基本数据类型,你可以使用Intent.putExtras()的方式。
为了传递那些需要持久化的基本数据类型,你可以使用Preferences存储机制。

非持久化的对象

为了短时间内共享那些用户自定义的非持久化的对象,我们推荐以下方案:

单例模式

你可以利用应用程序中使用单例模式运行在同一个进程当中的组件,这个类被设计成只有一个实例,它有诸如getInstance()这样的静态方法以返回一个实例;在这个方法被首次调用的时候,它就建立一个全局的实例。因为所有的调用者得到的是同一个实例,因此他们可以使用这点进行交互。例如activityA的到这个实例,同时调用了setValue(3);不一会activityB得到这个实例,同时调用getValue()得到了上次设置的值。

一个公共的静态字段或者方法

一种能够使得数据能够被不同Activity或者Service访问得到的另外方案就是使用公共静态的字段或者方法。在你应用程序当中其他的类都可以访问这些静态的字段。为了共享一个对象,一个Activity可以创建这个对象同时设置静态字段,其他想访问这个对象的Activity只需要访问这个静态的字段即可。

使用弱引用的HashMap

你可以使用一个弱引用的HashMap存储对象,同时使用相应的键值进行访问操作。当一个Activity想传递一个对象到另外一个Activity时,它仅仅需要将对象放入到HashMap当中,同时通过intent extras传递键值(这个键值是基于容器状况和时间戳而设置的,是独一无二的)到接受Activity。这个接收Activity通过这个键值就可以访问到这个对象。

持久化对象

即使应用程序现在正在运行,系统可能结束它的进程稍后在重新启动它。如果你有数据需要从一个Activity传递到另外一个,你就需要重新获得哪些被之前的Activity被告知结束之前保存的数据。
为了分享用户自定义的持久化复杂数据,我们推荐以下方式:
  • Application Preferences
  • Files
  • contentProviders
  • SQLite DB

如果共享数据需要在应用结束的时候保存的话,可以将这些数据保存到例如Application Preferences,SQLite DB,Files,contentProviders这样的持久化容器当中。关于如何使用这些组件的一些细节,你可以参考Data Storege。

四、如何在一个Activity没有启动之前判断它是否已经在运行当中?

开启一个没有运行的新Activity或者将一个已经后台运行的Activity带到栈的最前面的一半机制是在调用startActivity()时使用NEW_TASK_LAUNCH标志。

五、如果一个Activity启动了一个远程的服务,有没有一种方法让Service中的数据传回Activity?

你可以查看Service中关于客户端如何与Servie交互的例子。你可以好好的利用本地组件调用同一个线程当中的Service交互比远程调用简单的多的事实,查看一下Local Service Sample。在一些列子当中像单例模式这样的技术,也是非常有意义的。

六、如何避免得到应用程序没有响应的对话框?

请查阅Designing for Responsiveness 文档。

七、一个应用程序如何才能知道一个包的添加或删除?

无论在什么时候,当一个包被添加上后,系统都会广播一个带有PACKAGE_ADDED的intent的广播。类似的,在一个包被删除之后,同样会得到一个PACKAGE_REMOVED 广播。为了能够接收到这些广播,你应该写如下代码:

        <receiver android:name ="com.android.samples.app.PackageReceiver">
            <intent-filter>
             <action android:name="android.intent.action.PACKAGE_ADDED"/>
              <action android:name="android.intent.action.PACKAGE_REMOVED"/>            
            
              <data android:scheme="package" />
            </intent-filter>
        </receiver>

在这里,包接收者是一个广播接收者类。每次一个应用的包安装或者卸载,它的onReceive()方法都会被激发执行。

你可能感兴趣的:(”Android Application Framework FAQ“关于Android框架的常见问题)