android中application 关于全局变量

android编程中,application这样的名词似乎变得那样的不常见,而让大家更为熟悉的是activity、intent、provider、broadcast和service。但其实android中的application也有着它自身的用处。

  打开manifest文件,会看到有一个application配置标签,这就是有关application的使用了。那究竟application有什么用处呢?来看看SDK中是如何描述的:

 

 Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's < application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.

 

  就是说application是用来保存全局变量的,并且是在package创建的时候就跟着存在了。所以当我们需要创建全局变量的时候,不需要再像 j2se那样需要创建public权限的static变量,而直接在application中去实现。只需要调用Context的 getApplicationContext或者Activity的getApplication方法来获得一个application对象,再做出相应的处理。

 

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button 
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

 

 

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="second"
    />
</LinearLayout>

 

Se.java

package com.action;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Se extends Activity {
	private TextView tv;
	private MyApplication myApplication;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        myApplication = (MyApplication) getApplicationContext();
        
        tv = (TextView) findViewById(R.id.tv);
        tv.setText(myApplication.getName());
	}
}

 

创建MyApplication类别继承Application

MyApplication.java

package com.action;

import android.app.Application;

public class MyApplication extends Application {
	private String name;
	private String age;
	private String sex;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

 

Main.java

package com.action;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
	MyApplication myApplication;
	private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myApplication = (MyApplication) getApplicationContext();
        myApplication.setName("xingqiba");
        myApplication.setAge("22");
        myApplication.setSex("男");
        
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
		        intent.setClass(Main.this, Se.class);
		        startActivity(intent);
			}
		});
       
        
        
    }
}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.action" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name"
		android:name=".MyApplication">
		<activity android:name=".Main" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name=".Se" />
	</application>
</manifest>

 一定要记得在AndroidManifest文件里配置Application

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值