Android常用工具类之界面上的一些工具

来源:互联网 发布:导入mac的视频找不到了 编辑:程序博客网 时间:2024/04/30 07:05
对Toast的封装,获取线程ID,获取当前线程,设置背景
package com.ankoninc.utils;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.drawable.Drawable;import android.os.Handler;import android.os.Looper;import android.view.View;import android.view.WindowManager;import android.widget.Toast;/** * Created by zengna on 2015/12/29. */public class UIUtil {    private static final String TAG = "UIUtil";    private static Handler sHandler = new Handler(Looper.getMainLooper());    /**     * Retrieve the sHandler.     *     * @return the sHandler     */    public static Handler getHandler() {        return sHandler;    }    public static Thread getUIThread() {        return Looper.getMainLooper().getThread();    }    public static boolean isOnUIThread() {        return Thread.currentThread() == getUIThread();    }    /**     * Execute an runnable object on UI thread.     *     * @param action the object to execute.     */    public static void runOnUIThread(Runnable action) {        if (!isOnUIThread()) {            getHandler().post(action);        } else {            action.run();        }    }    public static boolean showDialogSafe(Dialog dialog) {        try {            dialog.show();            return true;        } catch (Exception e) {            // log detail informations            Log.w(null, e);            return false;        }    }    public static boolean dismissDialogSafe(DialogInterface dialog) {        if (dialog == null) {            return false;        }        try {            dialog.dismiss();            return true;        } catch (WindowManager.BadTokenException e) {            Log.w(null, e.getMessage());            return false;        } catch (IllegalStateException e) {            Log.w(null, e.getMessage());            return false;        } catch (Exception e) {            Log.w(null, e.getMessage());            return false;        }    }    public static void showToastSafe(Context context, int msgId) {        try {            Toast.makeText(context, context.getString(msgId), Toast.LENGTH_SHORT).show();        } catch (Exception e) {            Log.e(e.getMessage());        }    }    public static void showToastSafe(Context context, String msg) {        try {            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();        } catch (Exception e) {            Log.e(e.getMessage());        }    }    public static boolean showToastSafe(Toast toast) {        try {            toast.show();            return true;        } catch (Exception e) {            // log detail informations            Log.e(e.getMessage());            return false;        }    }    public static void setBackground(View view, Drawable drawable) {        if (null == view) {            return;        }        view.setBackground(drawable);    }}