android有没有和session,在Android下有类似于session的东西,叫做Application, getApplication()用法...

在Android上有类似于session的东西,叫做Application。

在Android上有类似于session的东西,叫做Application。

1、你可以新建一个类,例如:HelloWordApplication.java 必须extends Application(名字随你取)

在这个类里面设定你要全局的数据变量,例如:private String loginName;

然后生成它的get、set方法。

2、在AndroidManifest.xml文件中配置你的Application类,方法如下:

//略

就是在标签儿中增加android:name="HelloWordApplication"的属性配置。

3、在使用的时候:

HelloWordApplicationapplication = (HelloWordApplication)getApplication();

application.setLoginName("百度");

则就将"百度"保存到了Application里,其他地方要用的时候,application.getLoginName();就可以了。

Java代码  4dd986c1b61c3f29ed0a8144d51024c5.png

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();

}

}

Java代码  4dd986c1b61c3f29ed0a8144d51024c5.png

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(SystemtestActivity.this, PrintActivity.class);

startActivity(intent);

}

}

Java代码  4dd986c1b61c3f29ed0a8144d51024c5.png

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) {

super.onCreate(savedInstanceState);

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);

}

}

Java代码  4dd986c1b61c3f29ed0a8144d51024c5.png

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) {

super.onCreate(savedInstanceState);

final MySystemAppcation application = (MySystemAppcation) this

.getApplication();

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

}

}

Java代码  4dd986c1b61c3f29ed0a8144d51024c5.png

package="com.hyzing"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".SystemtestActivity"

android:label="@string/app_name" >

android:name=".PrintActivity"

android:label="@string/app_name" >

android:name=".PrintAgainActivity"

android:label="@string/app_name" >

原文:http://www.cnblogs.com/duanxz/p/4379800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值