using global variable in android extends application

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

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");
原文地址:https://www.cnblogs.com/tuolin/p/2043237.html