在Android下有类似于session的东西,叫做Application, getApplication()用法
在Android上有类似于session的东西,叫做Application。
在Android上有类似于session的东西,叫做Application。
1、你可以新建一个类,例如:HelloWordApplication.java 必须extends Application(名字随你取)
在这个类里面设定你要全局的数据变量,例如:private String loginName;
然后生成它的get、set方法。
2、在AndroidManifest.xml文件中配置你的Application类,方法如下:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="HelloWordApplication">
<activity >
//略
</activity>
</application>
就是在<application>标签儿中增加android:name="HelloWordApplication"的属性配置。
3、在使用的时候:
HelloWordApplicationapplication = (HelloWordApplication)getApplication();
application.setLoginName("百度");
则就将"百度"保存到了Application里,其他地方要用的时候,application.getLoginName();就可以了。
android getApplication()使用
- package com.hyzing;
- import android.app.Application;
- public class MySystemAppcation 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();
- }
- }
- package com.hyzing;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- public class SystemtestActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.main);
- MySystemAppcation application = (MySystemAppcation) this
- .getApplication();
- System.out.println("++++++++++++++"+application);
- Log.i("data", "===============" + application.getCurIndex());
- application.setCurIndex(5);
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putString("checkIn", "0");
- bundle.putBoolean("managerUser", true);
- intent.putExtras(bundle);
- intent.setClass(<span style="color: #ff0000;">SystemtestActivity.this</span>, PrintActivity.class);
- startActivity(intent);
- }
- }
- package com.hyzing;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- public class PrintActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- <span style="color: #ff0000;">super.onCreate(savedInstanceState);</span>
- MySystemAppcation application = (MySystemAppcation) this
- .getApplication();
- Log.i("data", "" + application.getCurIndex());
- application.setCurIndex(6);
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putString("checkIn", "0");
- bundle.putBoolean("managerUser", true);
- intent.putExtras(bundle);
- intent.setClass(PrintActivity.this, PrintAgainActivity.class);
- startActivity(intent);
- }
- }
- package com.hyzing;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class PrintAgainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- <span style="color: #ff0000;">super.onCreate(savedInstanceState);</span>
- final MySystemAppcation application = (MySystemAppcation) this
- .getApplication();
- Log.i("data", "" + application.getCurIndex());
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.hyzing"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="15" />
- <application android:name=".MySystemAppcation"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".SystemtestActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".PrintActivity"
- android:label="@string/app_name" >
- </activity>
- <activity
- android:name=".PrintAgainActivity"
- android:label="@string/app_name" >
- </activity>
- </application>
- </manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。