Interface to global information on an application environment. This was an abstract class whose implementation was provided by the Android system. It allows access to application-specific resources and classes, as-well-up-calls for application-level operations such As launching activities, broadcasting and receiving intents
is an interface for accessing global information (image resources, information resources), and some of the more commonly used components inherit the context interface.
Application
Like Activity,service, a system component of the Android framework, a Application object is created when the Android program is launched to store some information about the system. Usually we do not need to specify a application, then the system will automatically help us to create, if you need to create their own application, it is easy to create a class to inherit application and register in manifest application tag (just add a name attribute to the application tag to set the name of your application).
The Android system creates an object of the application class for each program runtime and creates only one, so application can be said to be a singleton (singleton) A class of patterns. And the life cycle of the Application object is the longest in the whole program, and its life cycle is equal to the life cycle of the program. Because it is a global singleton, the objects obtained in different activity,service are the same object. So through the application to do some, data transfer, data sharing, such as data caching operations.
Below we use an example to realize the application data sharing.
1, we create an object, inherit application
package com.example.learncontext;import android.app.Application;publicclass App extends Application { private"default"; publicgetName() { return name; } publicvoidsetName(String name) { this.name = name; }}
2. We need to configure Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.example.learncontext"android:versioncode="1"android: Versionname="1.0" > <uses-sdkandroid:minsdkversion="8"android:targetsdkversion ="/>" <applicationandroid:name=". App "android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label="@string/app_name"android:theme="@style/apptheme" > <activityandroid:name=". Mainactivity "android:label=" Main1 " > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activityandroid:name=". Bmainactivity "android:label=" main2 " > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </Application></manifest>
12 lines in the code, here we need to configure the path where we create the Application object.
Code 17 to 35 lines, we can see the configuration of 2 activity, and are the entry program, see Intent-filter in the code. The goal is to display 2 app portal icons on your phone for easy testing of application data sharing.
3. Below is the 2 Activity view view with 1 TextView, 1 editview, 1 button per view page added
<Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Tools:context=". Bmainactivity "> <textview android:id = "@+id/textview1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/hello_world" /> <edittext android:id = "@+id/edittext1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignleft
= "@+id/textview1" android:layout_below = "@+id/textview1" android:layout_margintop = "27DP" android:ems = "ten" /> <button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignleft = "@+id/edittext1" android:layout_below = "@+id/edittext1" android:layout_marginleft =" 18DP " android:layout_margintop = "28DP" android:text = "button" /> </relativelayout>
<Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Tools:context=". Bmainactivity "> <textview android:id = "@+id/textview1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/hello_world" /> <edittext android:id = "@+id/edittext1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignleft
= "@+id/textview1" android:layout_below = "@+id/textview1" android:layout_margintop = "27DP" android:ems = "ten" /> <button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignleft = "@+id/edittext1" android:layout_below = "@+id/edittext1" android:layout_marginleft =" 18DP " android:layout_margintop = "28DP" android:text = "button" /> </relativelayout>
4. Below is the code in 2 activity
PackageCom.example.learncontext;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public class bmainactivity extends Activity { PrivateTextView TextView;PrivateEditText EditText;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_bmain); TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1); EditText = (editText) Findviewbyid (R.ID.EDITTEXT1); Textview.settext ("The shared data obtained is:"+ (APP) Getapplicationcontext ()). GetName ()); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {(APP) Getapplicationcontext ()). SetName (EditText. GetText (). Tostri Ng ()); Textview.settext ("The shared data obtained is:"+ (APP) Getapplicationcontext ()). GetName ()); } }); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.bmain, menu);return true; }}
PackageCom.example.learncontext;Importandroid.app.Activity;ImportAndroid.app.Application;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public class mainactivity extends Activity { PrivateTextView TextView;PrivateEditText EditText;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1); EditText = (editText) Findviewbyid (R.ID.EDITTEXT1); Textview.settext ("The shared data obtained is:"+ (APP) Getapplicationcontext ()). GetName ()); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {(APP) Getapplicationcontext ()). SetName (EditText. GetText (). Tostri Ng ()); Textview.settext ("The shared data obtained is:"+ (APP) Getapplicationcontext ()). GetName ()); } }); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }}
30 lines of code in the program, ((APP) Getapplicationcontext ()), strong to the App object, and then get the properties in the object, through the put or get method to achieve data sharing storage and read operations.
5, the test diagram is as follows
Android-Context (Application application)