您的位置:首页 > 移动开发

using global variable in android extends application

2011-05-11 14:57 381 查看
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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: