• 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>


     

  • 相关阅读:
    【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)
    【SignalR学习系列】5. SignalR WPF程序
    小程序商城Mall,打造最佳SpringCloudAlibaba最佳实践
    《SpringCloudDubbo开发日记》(一)Nacos连官方文档都没写好
    Java后端开发工程师是否该转大数据开发?
    动态生成简约MVC请求接口|抛弃一切注解减少重复劳动吧
    主键生成器效率提升方案|基于雪花算法和Redis控制进程隔离
    高并发场景-请求合并(二)揭秘HystrixCollapser-利用Queue和线程池异步实现
    使用TiDB把自己写分库分表方案推翻了
    JMETER并发压测-自定义不同请求参数
  • 原文地址:https://www.cnblogs.com/riskyer/p/3231041.html
Copyright © 2020-2023  润新知