一、概述:
1、在清单文件里注册
android:allowBackup="true"
android:name="BaseApplication"
2、在application里可以如何获得 context
private static BaseApplication mApplication;
public static Context getContext(){
return mApplication;
}
3、可以根据application写个工具类
二、Application代码:
/**
* @描述 应用
* @项目名称 App_Shop
* @包名 com.android.shop
* @类名 BaseApplication
* @author chenlin
* @date 2015-3-26 下午9:19:13
*/
public class BaseApplication extends Application {
private static BaseApplication mApplication;
private static int mMainTid;//主线程id
private static Handler mHandler;//主线程处理handler
@Override
public void onCreate() {
mApplication = this;
mMainTid = android.os.Process.myTid();
mHandler = new Handler();
super.onCreate();
}
public static Context getContext(){
return mApplication;
}
public static int getMainTid() {
return mMainTid;
}
public static Handler getHandler() {
return mHandler;
}
}
三、工具类
/**
* @描述 商城工具类
* @项目名称 App_Shop
* @包名 com.android.shop.helper
* @类名 UIUtils
* @author chenlin
* @date 2015-3-26 下午9:18:40
*/
public class Util {
/**
* 获得字符自组
*
* @param resId
* @return
*/
public static String[] getStringArray(int resId) {
return getResource().getStringArray(resId);
}
/**
* 获得之源
*
* @return
*/
public static Resources getResource() {
return BaseApplication.getContext().getResources();
}
/**
* 获得context
*
* @return
*/
public static Context getContext() {
return BaseApplication.getContext();
}
/**
* 把dip转化为像素
*
* @param dip
* @return
*/
public static int dip2px(int dip) {
return (int) (getResource().getDisplayMetrics().density * dip + 0.5f);
}
/**
* 把像素转化为dip
*
* @param px
* @return
*/
public static int px2dip(int px) {
return (int) (px / getResource().getDisplayMetrics().density + 0.5f);
}
/**
* 让在主线程里运行
*
* @param runnable
*/
public static void runOnUiThread(Runnable runnable) {
if (android.os.Process.myTid() == BaseApplication.getMainTid()) {
runnable.run();// 表示在主线程里运行
} else {
BaseApplication.getHandler().post(runnable);// 在子线程里运行
}
}
/**
* 加载view
*
* @param layoutId
* @return
*/
public static View inflate(int layoutId) {
return View.inflate(getContext(), layoutId, null);
}
/**
* 获得尺寸
*
* @param resID
* @return
*/
public static int getDimen(int resID) {
return (int) getResource().getDimension(resID);
}
/**
* 开启activity
* @param intent
*/
public static void startActivity(Intent intent){
//如果不在activity里去打开activity是不行的,需要指定任务栈,像在fragment里
if(BaseActivity.activity == null){
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
}else {
BaseActivity.activity.startActivity(intent);
}
}
/**
* 获得drawable
* @param resId
* @return
*/
public static Drawable getDrawable(int resId) {
return getResource().getDrawable(resId);
}
}
———————————————————————
有需求者请加qq:136137465,非诚勿扰
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–