现在的位置: 首页 > 综合 > 正文

android 全局变量定义及使用方法

2018年09月04日 ⁄ 综合 ⁄ 共 2431字 ⁄ 字号 评论关闭

      因为现在做的项目用到了这个全局变量,我也是新手以前都没怎么接触过,现在自己学习了一下,在这里写一下,一来作为学习过程的记录,二来供和我一样的新人做个小小参考! 

     第一步:创建个 App类 继承   Application(import android.app.Application;)

        这还有一个动作:就是要在manifest文件中 Application 下 声明下App 类 ;

<application
        android:name=".app.App"

     第二步:运用get set 方法给想要设置的全局变量赋值 ,

     第三步:在需要全局变量的类 实例化 App 对象调用get方法即可 ,(App app = (App))

   代码:

   

/**
 * 父类已经实现了位置和网络的监控 此类实现访问网络对定时任务
 * 
 * @author james
 * 
 */
public class App extends Application {
	private final static String TAG = "App";

	private DataBase dataBase;

	private User loginUser;// 登陆用户

	private Product product;
	// 网络管理
	private NetworkMng networkMng;

	@Override
	public void onCreate() {
		Log.d(TAG, "创建应用:" + System.currentTimeMillis() + "," + this);

		super.onCreate();
		dataBase = new DataBase(this);

		// 将上次登陆用户作为默认用户记载
		UserDao userDao = new UserDao(this);
		loginUser = userDao.getLastUser();

		// 开始监控网络
		// this.networkMng = new NetworkMng(this);
		// this.networkMng.startReceive();
	}

	@Override
	public void onTerminate() {
		Log.d(TAG, "弹出应用:" + System.currentTimeMillis());

		// this.networkMng.stopReceive();

		dataBase.close();
		super.onTerminate();
	}

	public DataBase getDataBase() {
		return dataBase;
	}

	public NetworkMng getNetworkMng() {
		return networkMng;
	}

	public User getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(User loginUser) {
		this.loginUser = loginUser;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}

调用部分代码:

 

	App app = (App) context.getApplicationContext();
		User user = app.getLoginUser();
		ApplyDao dao = new ApplyDao(context);

		// 状态判断
		List<Object> args = new ArrayList<Object>();
		args.add(new Object[] { "type", "=", "in" });
		args.add(new Object[] { "species", "=", "prod" }); // 判断为商品还是辅料
		args.add("|");
		args.add(new Object[] { "state", "=", "wait_picking" }); // 等待入库
		args.add(new Object[] { "state", "=", "underd_way" }); // 入库中

		// 获取未完成入库申请单据信息
		OpenErpConnect connect = new OpenErpConnect(user.getUserID(),
				user.getPassword());

        Application类方法:

            怎么说呢,这些方法是在程序运行不同状态的时候会调用,我感觉很有用处,具体怎么运用,用来干什么!我认为值得想想!

Public Methods
void onConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running.
void onCreate()
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
void onLowMemory()
This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
void onTerminate()
This method is for use in emulated process environments.
void onTrimMemory(int
level)
Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.

      

抱歉!评论已关闭.