zoukankan      html  css  js  c++  java
  • Application使用示例

    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>


     

  • 相关阅读:
    < java.lang >-- StringBuilder字符串缓冲区
    Integer对象
    < java.lang >-- StringBuffer字符串缓冲区
    < java.lang >-- String字符串
    单例设计模式:★★★★★
    线程同步 Lock接口
    POJ 3254 Corn Fields (状压dp)
    Codeforces 583D. Once Again... (LIS变形)
    Light oj 1005
    Codeforces 543D. Road Improvement (树dp + 乘法逆元)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3231041.html
Copyright © 2011-2022 走看看