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都在应用主线程当中吗?
三、如何在不同的Acvitiy或者是Service当中传递复杂数据类型?
基本数据类型
非持久化的对象
单例模式
一个公共的静态字段或者方法
使用弱引用的HashMap
持久化对象
- Application Preferences
- Files
- contentProviders
- SQLite DB
如果共享数据需要在应用结束的时候保存的话,可以将这些数据保存到例如Application Preferences,SQLite DB,Files,contentProviders这样的持久化容器当中。关于如何使用这些组件的一些细节,你可以参考Data Storege。
四、如何在一个Activity没有启动之前判断它是否已经在运行当中?
五、如果一个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()方法都会被激发执行。
- 1楼su1216昨天 13:02
- 第四条答案什么意思n使用NEW_TASK_LAUNCH如何检查?
- Re: yangzl2008昨天 14:19
- 回复su1216这个用法已经过时了