Application是Android程序的主入口。下面详细介绍Application。
一.简单使用
Application实现类代码
package com.wjn.lubandemo.application;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
public class MyApplication extends Application {
/**
* attachBaseContext方法
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
Log.d("MyApplication", "attachBaseContext方法执行");
}
/**
* onCreate方法
*/
@Override
public void onCreate() {
super.onCreate();
Log.d("MyApplication", "onCreate方法执行");
}
/**
* onConfigurationChanged方法
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("MyApplication", "onConfigurationChanged方法执行");
}
/**
* onLowMemory方法
*/
@Override
public void onLowMemory() {
super.onLowMemory();
Log.d("MyApplication", "onLowMemory方法执行");
}
/**
* onTrimMemory方法
*/
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
Log.d("MyApplication", "onTrimMemory方法执行");
}
/**
* onTerminate方法
*/
@Override
public void onTerminate() {
super.onTerminate();
Log.d("MyApplication", "onTerminate方法执行");
}
}
清单文件配置代码
<application
android:name=".MyApplication"
...
</application>
常用方法讲解
1.attachBaseContext(Context base)
Set the base context for this ContextWrapper. All calls will then be delegated to the base context.
Throws IllegalStateException if a base context has already been set.
在onCreate方法之前调用。
2.onCreate()
Called when the application is starting,
before any activity, service,or receiver objects (excluding content providers) have been created.
Android程序的主入口,一般进行项目的初始化操作。但耗时操作可能影响程序启动速度。
3.onConfigurationChanged(Configuration newConfig)
屏幕旋转时调用。
4.onLowMemory()
Android 系统整体内存较低时调用。
5.onTrimMemory(int level)
通知应用程序内存使用情况。每次退出APP时调用
6.onTerminate()
This method is for use in emulated process environments.
It will never be called on a production Android device,
该方法只用于Android模拟器上,手机上永远不会调用。
二.使用注意事项
1.初始化时机
项目中一般的初始化都会放到Application中的onCreate方法中,但是会有这样的问题。由于项目中有好几个进程(推送等)导致Application的onCreate方法会执行多遍。这显然不是我们想要的,怎样避免呢?
可以获取线程名,通过线程名与包名对比来判断当前进程。
代码
package com.wjn.lubandemo.application;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
public class MyApplication extends Application {
/**
* onCreate--->程序创建的时候执行
*/
@Override
public void onCreate() {
super.onCreate();
Context context = getApplicationContext();
boolean isMainProcess = (context != null && TextUtils.equals(context.getPackageName(), getCurrentProcessName(context)));
if (isMainProcess) {
Log.d("MyApplication", "当前线程应该正常初始化各种方法");
}
}
/**
* 获取当前进程
*/
private String getCurrentProcessName(Context context) {
int pid = android.os.Process.myPid();
String processName = "";
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = manager.getRunningAppProcesses();
if (list != null) {
for (ActivityManager.RunningAppProcessInfo processInfo : list) {
if (processInfo != null && processInfo.pid == pid) {
processName = processInfo.processName;
}
}
}
return processName;
}
}