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.
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.
It depends on the type of data that you want to share:
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.
For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:
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.
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.
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.
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:
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.
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.
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.
Please read the Designing for Responsiveness document.
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 | 一个应用程序如何才能知道一个包的添加或删除? |
如果共享数据需要在应用结束的时候保存的话,可以将这些数据保存到例如Application Preferences,SQLite DB,Files,contentProviders这样的持久化容器当中。关于如何使用这些组件的一些细节,你可以参考Data Storege。
你可以查看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>