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>
Application使用示例的更多相关文章
- Content-Type: application/vnd.ms-excel">
如果要将查询结果导出到Excel,只需将页面的Context-Type修改一下就可以了: header( "Content-Type: application/vnd.ms-excel& ...
- iOS开发之 Xcode 6 创建一个Empty Application
参考链接http://jingyan.baidu.com/article/2a138328bd73f2074b134f6d.html Xcode 6 正式版如何创建一个Empty Applicatio ...
- NFC(7)向NFC硬件写入数据的两个示例(nfc硬件启动android应用,nfc硬件打开uri)
向NFC标签写入数据基本步骤 1,获取Tag对象 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 2,判断NFC标签的数据类型(通 ...
- 【Android 应用开发】 Application 使用分析
博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...
- yarn application命令介绍
yarn application 1.-list 列出所有 application 信息 示例:yarn application -list 2.-appStates <Stat ...
- Application 使用分析
一. Application 分析 1. Application 简介 (1) Application 概念 Application 概念 : Application 属于组件范畴; -- 本质 : ...
- mongodb 速成笔记
以下环境为mac osx + jdk 1.8 + mongodb v3.2.3 一.安装 brew安装方式是mac下最简单的方式 brew update brew install mongodb 其它 ...
- mha安装使用手册
mha安装使用手册 注:目前mha最新的版本代码已经不放到google code网站了,而是放在github上,最新的版本为0.57,github链接如下: mha manager:https://g ...
- 原生Ajax封装随笔
XMLHttpRequest 对象用于和服务器交换数据.我们使用 XMLHttpRequest 对象的 open() 和 send() 方法: open(method,url,async) metho ...
随机推荐
- [转]left join,right join,inner join区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- Django生产环境的部署-Apache-mod_wsgi
httpd.conf配置 ServerSignature On ServerTokens Full Define APACHE24 Apache2.4 Define SERVER_BASE_DIR & ...
- PCB设计之原理图绘制笔记
02原理图工作环境设置原理图画布由画布和边界(Border)构成.可以通过DocumentOptions设置(快捷键DO).DocumentOptions设置--------------------- ...
- ASP.NET 共用类库
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- 2015年9月29日 sql 触发器
触发器(trigger):当有关联操作的时候使用(级联操作),属于ddl关键字. eg:下订单时,创建中的商品数量要减少:退票时,总的票数要增加. 在订单上建立触发器 ...
- Docker系列
Docker学习系列(五):Dockerfile文件 什么是Dockerfile? 它是一个名称为Dockerfile的文件 它是一个脚本文件,由一系列命令和参数构成 Dockerfile是自动构建d ...
- Pentaho Data Integration (三) Pan
官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...
- 转:PHP – Best Practises
原文来自于:http://thisinterestsme.com/php-best-practises/ There are a number of good practises that you s ...
- BZOJ 1707: [Usaco2007 Nov]tanning分配防晒霜
Description 奶牛们计划着去海滩上享受日光浴.为了避免皮肤被阳光灼伤,所有C(1 <= C <= 2500)头奶牛必须在出门之前在身上抹防晒霜.第i头奶牛适合的最小和最 大的SP ...
- 【技术贴】解决vss中提交pdf下载打开空白乱码
vss客户端需要安装一个Vss2005的补丁程序,而且之前上传的pdf文件重新删掉,再次上传进Vss中,再下载打卡就ok了. 补丁名称vs80-kb943847-x86-intl.exe 别人的csd ...