application android 部分转载

来源:互联网 发布:不用u盘安装ubuntu 编辑:程序博客网 时间:2024/04/30 09:24


Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally usesContext.getApplicationContext() when first constructing the singleton.


对于那些需要保持全球应用程序的状态基类指定它的名字的AndroidManifest.xml<application>标签,这将导致实例化这个类您的应用程序/过程创建可以提供自己的实现。

通常是没有必要的子类的应用大多数情况下静态singletons 可以在一个更加模块化方式提供相同的功能如果singletons 需要a global context例如注册广播接收机检索功能可以给其中内部使用Context.getApplicationContext上下文时,首先建设的单身


http://apps.hi.baidu.com/share/detail/34698960
public class TestApplication extends Application {  private int curIndex;  public int getCurIndex() {  return curIndex;  }  public void setCurIndex(int curIndex) {  this.curIndex = curIndex;  }  @Override  public void onCreate() {  super.onCreate();  }  @Override  public void onTerminate() {  super.onTerminate();  }  }


application中有一个curIndex和setter getter方法。

  第一个acitivty中对application进行的操作:

  Java代码:  

  TestApplication application = (TestApplication) this.getApplication();

  Log.i("data", ""+application.getCurIndex());

  application.setCurIndex(5);

  第二个Activity:

  Java代码:

  TestApplication application = (TestApplication)this.getApplication();

  Log.i("data", ""+application.getCurIndex());

  application.setCurIndex(6);

  第三个Activity:

  Java代码

  final TestApplication application = (TestApplication) this.getApplication();

  Log.i("data", ""+application.getCurIndex());

  在运行过程中,每一次都kill掉对应的Activity,再进入下一个Activity。运行的结果如下:


http://blog.csdn.net/maxleng/article/details/5621345

Android提供给开发程序员的概念空间中Application只是一个松散的表征概念,没有多少实质上的表征。在Android实际空间中看不到实际意义上的应用程序的概念,即使有一个叫Application的类,这个也就是个应用程序上下文状态,是一个极度弱化的概念。Application只是一个空间范畴的概念,Application就是Activity,Service之类的组件上下文描述。Application并不是Android的核心概念,而Activity才是Android的核心概念。

    从Android的SDK文档中,我们知道一般情况Android应用程序是由以下四种组件构造而成的:Activity,Broadcast Intent Receiver,服务(Service),内容提供器(Content Provider)。我们可以使用下面的图来表示一下Android的概念空间。这些组件依附于应用程序中,应用程序并不会一开始就建立起来,而是在这些组件建立起来后,需要运行时,才开始建立应用程序对象。

2.1应用进程名称

    为什么要从应用进程名称开始?作为内核研究,我们还是回到问题的最本质处:不管Activity,Service等组件如何设计和运行,它要提供服务,就必须要依附在Linux的进程上,建立消息循环,组件才能够真正的运作。Activity实例是如何Hosting在Linux进程上的?这个是我们首先想要弄明白的。

我们在的项目中看到android:process="string"这个定义。

<application android:<="" font="">allowClearUserData=["true" | "false"] 
android:
allowTaskReparenting=["true" | "false"] 
android:
backupAgent="string" 

android:label="string resource" 
android:manageSpaceActivity="string" 
android:name="string" 
android:permission="string" 
android:persistent=["true" | "false"] 
android:process="string" 
android:restoreAnyVersion=["true" | "false"] 
android:taskAffinity="string" 
android:theme="resource or theme" > 
    . . . 

在SDK用已经描述的很清楚到了。

android:process

The name of a process where all components of the application should run. Each component can override this default by setting its own process attribute.

By default, Android creates a process for an application when the first of its components needs to run. All components then run in that process. The name of the default process matches the package name set by the element.

By setting this attribute to a process name that's shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.

为什么要提出这么一个定义?android:process名称。

    默认状态下,Activity Manager Service在应用程序的第一个组件需要运行时将会为应用程序建立一个进程,而这个进程的名字就是android:process=”string”所指定,缺省的是应用程序包的名字。该进程一旦建立,后面的该应用的组件都将运行在该进程中,他们绑定的根据就是这个Android:Process指定的名称,因为在他们都在同一个应用程序包里,也就具有了同样的进程名字,于是他们都托管在了同一进程中。组件将通过ClassLoader从Package中获取到应用程序的信息。

    在建立Actvitiy时,如果在应用进程端没有应用对象,系统在该过程中利用makeApplication建立一个Application对象,实例化"android.app.Application",建立一个应用程序上下文完成例如资源,package等信息管理


原创粉丝点击