MainActivity如下:

  1. package cn.testapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.widget.Button;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. /**
  9. * Demo描述:
  10. * Application的基本使用
  11. * 1 自定义OurApplication继承自Application
  12. * 2 为AndroidManifest.xml下application结点新加个name属性
  13. * name的具体值即为OurApplication的路径
  14. *
  15. * 简单流程:
  16. * 在MainActivity中点击按钮跳转到AnotherActivity.在AnotherActivity
  17. * 中修改了OurApplication中所保存的值.然后按下Back键,回到MainActivity.
  18. * 此时监测OurApplication中所保存的值.
  19. */
  20. public class MainActivity extends Activity {
  21. private Button mButton;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. init();
  27. }
  28. private void init(){
  29. mButton=(Button) findViewById(R.id.button);
  30. mButton.setOnClickListener(new OnClickListener() {
  31. @Override
  32. public void onClick(View view) {
  33. Intent intent=new Intent(MainActivity.this, AnotherActivity.class);
  34. startActivity(intent);
  35. }
  36. });
  37. }
  38.  
  39. @Override
  40. protected void onRestart() {
  41. super.onRestart();
  42. OurApplication ourApplication=(OurApplication) getApplication();
  43. System.out.println("MainActivity中得到新的Password:"+ourApplication.getPassword());
  44. }
  45.  
  46. }

AnotherActivity如下:

  1. package cn.testapplication;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class AnotherActivity extends Activity{
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.another);
  9. init();
  10. }
  11. private void init(){
  12. OurApplication ourApplication=(OurApplication) getApplication();
  13. System.out.println("AnotherActivity中得到原来的Password:"+ourApplication.getPassword());
  14. String newPassword="9527";
  15. ourApplication.setPassword(newPassword);
  16. System.out.println("AnotherActivity中设置新的Password:"+newPassword);
  17. }
  18. }

OurApplication如下:

  1. package cn.testapplication;
  2.  
  3. import android.app.Application;
  4.  
  5. public class OurApplication extends Application {
  6. private String applicationPassword;
  7. @Override
  8. public void onCreate() {
  9. super.onCreate();
  10. applicationPassword="007";
  11. }
  12. public void setPassword(String password){
  13. applicationPassword=password;
  14. }
  15. public String getPassword(){
  16. return applicationPassword;
  17. }
  18. }

AndroidManifest.xml如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.testapplication"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="8" />
  10.  
  11. <application
  12. android:name="cn.testapplication.OurApplication"
  13. android:allowBackup="true"
  14. android:icon="@drawable/ic_launcher"
  15. android:label="@string/app_name"
  16. android:theme="@style/AppTheme" >
  17. <activity
  18. android:name="cn.testapplication.MainActivity"
  19. android:label="@string/app_name" >
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22.  
  23. <category android:name="android.intent.category.LAUNCHER" />
  24. </intent-filter>
  25. </activity>
  26.  
  27. <activity android:name="cn.testapplication.AnotherActivity"></activity>
  28. </application>
  29. </manifest>

main.xml如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6.  
  7. <Button
  8. android:id="@+id/button"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Click Me"
  12. android:textSize="28sp"
  13. android:layout_centerInParent="true"
  14. />
  15.  
  16. </RelativeLayout>

another.xml如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6.  
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="Another Activity"
  11. android:textSize="28sp"
  12. android:layout_centerInParent="true"
  13. />
  14.  
  15. </RelativeLayout>

Application使用示例的更多相关文章

  1. Content-Type: application/vnd.ms-excel">

    如果要将查询结果导出到Excel,只需将页面的Context-Type修改一下就可以了:   header( "Content-Type: application/vnd.ms-excel& ...

  2. iOS开发之 Xcode 6 创建一个Empty Application

    参考链接http://jingyan.baidu.com/article/2a138328bd73f2074b134f6d.html Xcode 6 正式版如何创建一个Empty Applicatio ...

  3. NFC(7)向NFC硬件写入数据的两个示例(nfc硬件启动android应用,nfc硬件打开uri)

    向NFC标签写入数据基本步骤 1,获取Tag对象 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 2,判断NFC标签的数据类型(通 ...

  4. 【Android 应用开发】 Application 使用分析

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...

  5. yarn application命令介绍

    yarn application 1.-list     列出所有 application 信息    示例:yarn  application -list 2.-appStates <Stat ...

  6. Application 使用分析

    一. Application 分析 1. Application 简介 (1) Application 概念 Application 概念 : Application 属于组件范畴; -- 本质 : ...

  7. mongodb 速成笔记

    以下环境为mac osx + jdk 1.8 + mongodb v3.2.3 一.安装 brew安装方式是mac下最简单的方式 brew update brew install mongodb 其它 ...

  8. mha安装使用手册

    mha安装使用手册 注:目前mha最新的版本代码已经不放到google code网站了,而是放在github上,最新的版本为0.57,github链接如下: mha manager:https://g ...

  9. 原生Ajax封装随笔

    XMLHttpRequest 对象用于和服务器交换数据.我们使用 XMLHttpRequest 对象的 open() 和 send() 方法: open(method,url,async) metho ...

随机推荐

  1. [转]left join,right join,inner join区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  2. Django生产环境的部署-Apache-mod_wsgi

    httpd.conf配置 ServerSignature On ServerTokens Full Define APACHE24 Apache2.4 Define SERVER_BASE_DIR & ...

  3. PCB设计之原理图绘制笔记

    02原理图工作环境设置原理图画布由画布和边界(Border)构成.可以通过DocumentOptions设置(快捷键DO).DocumentOptions设置--------------------- ...

  4. ASP.NET 共用类库

    using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...

  5. 2015年9月29日 sql 触发器

    触发器(trigger):当有关联操作的时候使用(级联操作),属于ddl关键字. eg:下订单时,创建中的商品数量要减少:退票时,总的票数要增加.         在订单上建立触发器         ...

  6. Docker系列

    Docker学习系列(五):Dockerfile文件 什么是Dockerfile? 它是一个名称为Dockerfile的文件 它是一个脚本文件,由一系列命令和参数构成 Dockerfile是自动构建d ...

  7. Pentaho Data Integration (三) Pan

    官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...

  8. 转:PHP – Best Practises

    原文来自于:http://thisinterestsme.com/php-best-practises/ There are a number of good practises that you s ...

  9. BZOJ 1707: [Usaco2007 Nov]tanning分配防晒霜

    Description 奶牛们计划着去海滩上享受日光浴.为了避免皮肤被阳光灼伤,所有C(1 <= C <= 2500)头奶牛必须在出门之前在身上抹防晒霜.第i头奶牛适合的最小和最 大的SP ...

  10. 【技术贴】解决vss中提交pdf下载打开空白乱码

    vss客户端需要安装一个Vss2005的补丁程序,而且之前上传的pdf文件重新删掉,再次上传进Vss中,再下载打卡就ok了. 补丁名称vs80-kb943847-x86-intl.exe 别人的csd ...