封装RunOnUIThread方法

来源:互联网 发布:js隐藏元素 编辑:程序博客网 时间:2024/04/29 10:27
package com.example.desktop.gouwuche_demo.utils;/** * Created by Dash */import android.content.SharedPreferences;import android.graphics.drawable.Drawable;import android.view.View;/** * Created by Dash */public class CommonUtils {    public static final String TAG = "";//sp文件的xml名称    private static SharedPreferences sharedPreferences;    /**     * DashApplication.getAppContext()可以使用,但是会使用系统默认的主题样式,如果你自定义了某些样式可能不会被使用     * @param layoutId     * @return     */    public static View inflate(int layoutId) {        View view = View.inflate(MyApplication.getAppContext(), layoutId, null);        return view;    }    /**     * dip---px     *     * @param dip 设备独立像素device independent px....1dp = 3px 1dp = 2px 1dp = 1.5px     * @return     */    public static int dip2px(int dip) {        //获取像素密度        float density = MyApplication.getAppContext().getResources().getDisplayMetrics().density;        //        int px = (int) (dip * density + 0.5f);//100.6        return px;    }    /**     * px-dip     *     * @param px     * @return     */    public static int px2dip(int px) {        //获取像素密度        float density = MyApplication.getAppContext().getResources().getDisplayMetrics().density;        //        int dip = (int) (px / density + 0.5f);        return dip;    }    /**     * 获取资源中的字符串     * @param stringId     * @return     */    public static String getString(int stringId) {        return MyApplication.getAppContext().getResources().getString(stringId);    }    public static Drawable getDrawable(int did) {        return MyApplication.getAppContext().getResources().getDrawable(did);    }    public static int getDimens(int id) {        return MyApplication.getAppContext().getResources().getDimensionPixelSize(id);    }    /**     * sp存入字符串类型的值     * @param flag     * @param str     */    public static void saveSp(String flag, String str) {        if (sharedPreferences == null) {            sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE);        }        SharedPreferences.Editor edit = sharedPreferences.edit();        edit.putString(flag, str);        edit.commit();    }    public static String getSp(String flag) {        if (sharedPreferences == null) {            sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE);        }        return sharedPreferences.getString(flag, "");    }    public static boolean getBoolean(String tag) {        if (sharedPreferences == null) {            sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE);        }        return sharedPreferences.getBoolean(tag, false);    }    public static void putBoolean(String tag, boolean content) {        if (sharedPreferences == null) {            sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE);        }        SharedPreferences.Editor edit = sharedPreferences.edit();        edit.putBoolean(tag, content);        edit.commit();    }    /**     * 清除sp数据     * @param tag     */    public static void clearSp(String tag) {        if (sharedPreferences == null) {            sharedPreferences = MyApplication.getAppContext().getSharedPreferences(TAG, MyApplication.getAppContext().MODE_PRIVATE);        }        SharedPreferences.Editor edit = sharedPreferences.edit();        edit.remove(tag);        edit.commit();    }    /**     * 自己写的运行在主线程的方法     * 如果是主线程,执行任务,否则使用handler发送到主线程中去执行     *     *     * @param runable     */    public static void runOnUIThread(Runnable runable) {        //先判断当前属于子线程还是主线程        if (android.os.Process.myTid() == MyApplication.getMainThreadId()) {            runable.run();        } else {            //子线程            MyApplication.getAppHanler().post(runable);        }    }}
必须要初始化,并在清单文件中注册
package com.example.desktop.gouwuche_demo.utils;import android.app.Application;import android.content.Context;import android.os.Handler;import android.os.Process;/** * Created by DESKTOP on 2017/12/20. */public class MyApplication extends Application{    private static Context context;    private static Handler handler;    private static int mainId;    public static boolean isLoginSuccess;//是否已经登录的状态    @Override    public void onCreate() {        super.onCreate();        //关于context----http://blog.csdn.net/lmj623565791/article/details/40481055        context = getApplicationContext();        //初始化handler        handler = new Handler();        //主线程的id        mainId = Process.myTid();    }    /**     * 对外提供了context     * @return     */    public static Context getAppContext() {        return context;    }    /**     * 得到全局的handler     * @return     */    public static Handler getAppHanler() {        return handler;    }    /**     * 获取主线程id     * @return     */    public static int getMainThreadId() {        return mainId;    }}
 
原创粉丝点击