using global variable in android extends application

本文介绍如何在Android应用中通过继承Application类实现全局变量的设置与获取。具体包括配置AndroidManifest.xml文件,创建GucApplication类来管理全局Map类型变量,并在Activity中进行值的存取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.extends application,this class must in android pakage; and in AndroidManifest.xml set android:name="GucApplication ";


     
?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.guc.android" android:versionCode ="1" android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
< uses-permission android:name ="android.permission.INTERNET" />
< uses-permission android:name ="android.permission.CALL_PHONE" />
< uses-permission android:name ="android.permission.PROCESS_OUTGOING_CALLS" />
< uses-permission android:name ="android.permission.READ_PHONE_STATE" />
< application android:name ="GucApplication" android:icon ="@drawable/icon"
android:label
="@string/app_name"
android:debuggable
="true" >
< activity android:name ="WelcomeActivity" android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >

package com.guc.android;

import java.util.HashMap;
import java.util.Map;

import android.app.Application;
import android.content.Context;

public class GucApplication extends Application {
	private static GucApplication instance;
	private Map<String, String> appContext;
    public GucApplication() {
    	appContext=new HashMap<String, String>();
        instance = this;
    }
    public static Context getContext() {
        return instance;
    }
	public Map<String, String> getAppContext() {
		return appContext;
	}
	public void setAppContext(String key, String value) {
		appContext.put(key, value);
	}
}

2011051114510456.png

2.set value and get value 


     
Map < String, String > o = (Map < String, String > ) this .getListAdapter().getItem(position);
String archive
= o. get ( " fmember_id " );
GucApplication gucAppContext
= (GucApplication)getApplicationContext();
gucAppContext.setAppContext(
" archive " ,archive);

     
GucApplication gucAppContext = (GucApplication)getApplicationContext();
String archive
= gucAppContext.getAppContext(). get ( " archive " );
posted on 2011-05-11 14:57 庹林 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tuolin/archive/2011/05/11/2043237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值