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

对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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值