toast

public class MToast {

    public static void show(Context context, String message) {
        if (context == null || TextUtils.isEmpty(message))
            return;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List infos = am.getRunningTasks(1);
        if (infos != null && infos.get(0) != null
                && context.getPackageName().equals(infos.get(0).baseActivity.getPackageName())) {

            int duration = Toast.LENGTH_SHORT;
            if (message.length() > 15) {
                duration = Toast.LENGTH_LONG;//如果字符串比较长,那么显示的时间也长一些。
            }
            Toast.makeText(context, message, duration).show();
        }
    }

    public static void show(Context context, int resId) {
        if (context == null)
            return;
        show(context, context.getString(resId));
    }
}

你可能感兴趣的:(android)