一、共享全局数据
这个效果就是使用静态变量是一样的,但是其更符合android的架构体系。
class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
二、全局提示信息、对话框弹出:
public class BaseApp extends Application {
public Context context;
private Handler handler;
@Override
public void onCreate() {
super.onCreate();
context = getBaseContext();
handler = new Handler();
}
public void showToast(final int resId) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
}
});
}
public void showToast(final String text) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
});
}
}
AlertDialog.Builder(Context context)中的context不能使用getApplicationContext()获得的Context,而必须使用Activity的Context对象,因为只有一个Activity才能添加一个窗体。
三、其他
貌似还不够妙~!期待补充!!!
补充:
application context的生命周期是贯穿整个进程,Activity的context只是伴随Activity的生命周期;
分享到:
相关推荐
Android Application Development Android Application Development Android Application Development Android Application Development Android Application Development Android Application Development
A Study of Android Application Security 学术论文
android application
Android Application Security.pdf
文档详细介绍了Android的运行机制,Application类的作用和使用方法,读过之后终于不再一知半解了!
Android Application 对象介绍
Android application structure 12 Application signing 15 Data storage on the device 15 Crypto APIs 16 Device Administration 17 Summary 17 Chapter 2: Application Building Blocks 19 Application ...
Android Application Programming with OpenCV 英文版
professional android application development 英文版,权威android 开发指南,带例子
Android Application Development For Dummies
Professional Android Application Development--来自网络 仅供学习 如有侵权 请联系删除
Android 红皮书,从入门到精通,由浅入深,出门必备。
在android中必须要有一个application,那我们用第三方的时候多个application如何使用,这是一个demo,值得你看一下
Android Application插件例子程序
Android Application development for Dummies android 开发 适合初学者。
Android Application Secure Design/Secure Coding Guidebook 一书的代码
Android Application Development: Programming with the Google SDK 电子书 Rick Rogers (Author), John Lombardo (Author), Zigurd Mednieks (Author), G. Blake Meike (Author) Paperback: 336 pages Publisher: ...
If you’re an experienced developer, you can start creating robust mobile Android apps right away with this professional guide to Android 4 application development. Written by one of Google’s lead ...
Wrox Professional Android 2 Application Development
简单的android应用,模仿登陆界面,用于搜狐微博开放平台