自定义Application使用示例——几个Activity共同操作某个变量

MainActivity如下:

package cn.testapplication;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
/**
 * Demo描述:
 * Application的基本使用
 * 1 自定义OurApplication继承自Application
 * 2 为AndroidManifest.xml下application结点新加个name属性
 *   name的具体值即为OurApplication的路径
 *   
 * 简单流程:
 * 在MainActivity中点击按钮跳转到AnotherActivity.在AnotherActivity
 * 中修改了OurApplication中所保存的值.然后按下Back键,回到MainActivity.
 * 此时监测OurApplication中所保存的值.
 */
public class MainActivity extends Activity {
    private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, AnotherActivity.class);
                startActivity(intent);
			}
		});
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		OurApplication ourApplication=(OurApplication) getApplication();
    	System.out.println("MainActivity中得到新的Password:"+ourApplication.getPassword());
	}

}


AnotherActivity如下:

package cn.testapplication;
import android.app.Activity;
import android.os.Bundle;
public class AnotherActivity extends Activity{
   @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.another);
		init();
	}
    private void init(){
    	OurApplication ourApplication=(OurApplication) getApplication();
    	System.out.println("AnotherActivity中得到原来的Password:"+ourApplication.getPassword());
    	String newPassword="9527";
    	ourApplication.setPassword(newPassword);
    	System.out.println("AnotherActivity中设置新的Password:"+newPassword);
    }
}


OurApplication如下:

package cn.testapplication;

import android.app.Application;

public class OurApplication extends Application {
	private String applicationPassword;
	@Override
	public void onCreate() {
		super.onCreate();
		applicationPassword="007";
	}
	public void setPassword(String password){
		applicationPassword=password;
	}
	public String getPassword(){
		return applicationPassword;
	}
}


AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.testapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:name="cn.testapplication.OurApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.testapplication.MainActivity"
            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="cn.testapplication.AnotherActivity"></activity>
    </application>
</manifest>


main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:textSize="28sp"
        android:layout_centerInParent="true"
    />

</RelativeLayout>


another.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another Activity"
        android:textSize="28sp"
        android:layout_centerInParent="true"
    />

</RelativeLayout>


 

上一篇:用阿里云建网站三种方式(自助建站+模板建站+功能定制建站)


下一篇:SlidingMenu使用简介