Android Application的写法一

一、概述:

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入门)
——————————————————————–

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值