• 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");
  • 相关阅读:
    Java18(泛型,反射,内省)
    Java17(线程池、Runnable和Callable、Lock、信号量、任务调度、Timer)
    Javaday16(守护线程,线程同步,线程通讯)
    面试题错题集04(异常)
    面试题错题集03(数组)
    面试题错题集02(面向对象)
    Javaday15(Queue,Collections工具类,多线程)
    Java中的内部接口
    ISO14229:2013 之 通过标志读数据ReadDataByIdentifier (0x22)
    Oracle数据库迁移至PostgreSQL数据库问题及解决
  • 原文地址:https://www.cnblogs.com/tuolin/p/2043237.html
Copyright © 2020-2023  润新知