android中的全局对象非常类似于javaWeb中的Application域,除非是android应用程序清楚内存,否则全局对象将一直可以访问。

android中有一个application类,是基类。创建自己的应用的时候需要继承这个基类,并实现onCreate方法,该方法在application被调用的时候就会启动。

创建自己的application类:

package com.example.intent;import android.app.Application;public class MyApp extends Application {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}/** * @param args */@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();this.name="mayi";this.age=23;}}



创建了自己的app后,必须在AndroidManifest.xml中定义自己的应用,

  <application        android:name=".MyApp"     //定义自己的app        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity  在其他activity中调用app: package com.example.intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private MyApp myapp;private TextView textview;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);textview = (TextView)this.findViewById(R.id.msg);myapp = (MyApp)getApplication();  //获得APPtextview.setText("name:   "+myapp.getName()+"\n"+"age:    "+myapp.getAge());}}

更多相关文章

  1. Android(安卓)file类使用详解-SDcard
  2. Android(安卓)activity 详解一:activity的生命周期
  3. Android的MediaPlayer架构介绍
  4. Android(安卓)中webview与原生Java的 交互以及网页JS 与原生的交
  5. Android(安卓)init.rc文件解析过程分析
  6. Webview实现Android和JS通信
  7. 【转】Android(安卓)技术-- 图形系统详解
  8. 带weight的LinearLayout嵌套RecyclerView导致RecycleView执行多
  9. 创建 Android(安卓)库

随机推荐

  1. PANIC: Could not open AVD config file:
  2. Android利用Fiddler进行网络数据抓包
  3. Android(安卓)debug.keystore的密码
  4. Android(安卓)学习成品
  5. View Android(安卓)Source Code in Eclip
  6. Android(安卓)资源文件中的符号含义与说
  7. android获取gps坐标
  8. Android学习目录
  9. 安卓巴士Android开发神贴整理
  10. android 2.2 eclipse