码迷,mamicode.com
首页 > 其他好文 > 详细

13、显示管理和日志.avi

时间:2015-10-16 18:54:28      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

主要是介绍了两个工具类。
/**
 * 提示信息的管理
 */
public class PromptManager {
    private static ProgressDialog dialog;
    public static void showProgressDialog(Context context) {
        dialog = new ProgressDialog(context);
        dialog.setIcon(R.drawable.icon);
        dialog.setTitle(R.string.app_name);
        dialog.setMessage("请等候,数据加载中……");
        dialog.show();
    }
    public static void closeProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
    /**
     * 当判断当前手机没有网络时使用
     * 
     * @param context
     */
    public static void showNoNetWork(final Context context) {
        AlertDialog.Builder builder = new Builder(context);
        builder.setIcon(R.drawable.icon)//
                .setTitle(R.string.app_name)//
                .setMessage("当前无网络").setPositiveButton("设置"new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 跳转到系统的网络设置界面
                        Intent intent = new Intent();
                        intent.setClassName("com.android.settings""com.android.settings.WirelessSettings");
                        context.startActivity(intent);
                    }
                }).setNegativeButton("知道了"null).show();
    }
    /**
     * 退出系统
     * 
     * @param context
     */
    public static void showExitSystem(Context context) {
        AlertDialog.Builder builder = new Builder(context);
        builder.setIcon(R.drawable.icon)//
                .setTitle(R.string.app_name)//
                .setMessage("是否退出应用").setPositiveButton("确定"new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                        // 多个Activity——懒人听书:没有彻底退出应用
                        // 将所有用到的Activity都存起来,获取全部,干掉
                        // BaseActivity——onCreated——放到容器中
                    }
                })//
                .setNegativeButton("取消"null)//
                .show();
    }
    /**
     * 显示错误提示框
     * 
     * @param context
     * @param msg
     */
    public static void showErrorDialog(Context context, String msg) {
        new AlertDialog.Builder(context)//
                .setIcon(R.drawable.icon)//
                .setTitle(R.string.app_name)//
                .setMessage(msg)//
                .setNegativeButton(context.getString(R.string.is_positive), null)//
                .show();
    }
    public static void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
    public static void showToast(Context context, int msgResId) {
        Toast.makeText(context, msgResId, Toast.LENGTH_LONG).show();
    }
    // 当测试阶段时true
    private static final boolean isShow = true;
    /**
     * 测试用 在正式投入市场:删
     * 
     * @param context
     * @param msg
     */
    public static void showToastTest(Context context, String msg) {
        if (isShow) {
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        }
    }
}



/**
 * 日志记录
 * 
 */
public class LogUtil {
    /**
     * 开发阶段
     */
    private static final int DEVELOP = 0;
    /**
     * 内部测试阶段
     */
    private static final int DEBUG = 1;
    /**
     * 公开测试
     */
    private static final int BATE = 2;
    /**
     * 正式版
     */
    private static final int RELEASE = 3;
    /**
     * 当前阶段标示
     */
    private static int currentStage = DEVELOP;
    private static String path;
    private static File file;
    private static FileOutputStream outputStream;
    private static String pattern = "yyyy-MM-dd HH:mm:ss";
    static {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File externalStorageDirectory = Environment.getExternalStorageDirectory();
            path = externalStorageDirectory.getAbsolutePath() + "/zcw/";
            File directory = new File(path);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            file = new File(new File(path), "log.txt");
            android.util.Log.i("SDCAEDTAG"path);
            try {
                outputStream = new FileOutputStream(filetrue);
            } catch (FileNotFoundException e) {
            }
        } else {
        }
    }
    public static void info(String msg) {
        info(LogUtil.class, msg);
    }
    public static void info(Class clazz, String msg) {
        switch (currentStage) {
            case DEVELOP:
                // 控制台输出
                Log.i(clazz.getSimpleName(), msg);
                break;
            case DEBUG:
                // 在应用下面创建目录存放日志
                
                break;
            case BATE:
                // 写日志到sdcard
                Date date = new Date();
                String time = DateFormatUtils.format(date, pattern);
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    if (outputStream != null) {
                        try {
                            outputStream.write(time.getBytes());
                            String className = "";
                            if (clazz != null) {
                                className = clazz.getSimpleName();
                            }
                            outputStream.write(("    " + className + "\r\n").getBytes());
                            outputStream.write(msg.getBytes());
                            outputStream.write("\r\n".getBytes());
                            outputStream.flush();
                        } catch (IOException e) {
                        }
                    } else {
                        android.util.Log.i("SDCAEDTAG""file is null");
                    }
                }
                break;
            case RELEASE:
                // 一般不做日志记录
                break;
        }
    }
}




13、显示管理和日志.avi

标签:

原文地址:http://www.cnblogs.com/linmbbest/p/4885879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!