Android 常用功能代码块

    /**
     * @return String time format with:yyyy_MMdd_HHmmss.
     *//获取手机的第一次开机时间
     */
    public static String getBootTimeString() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(getRebootTime());
        String bootTimeStr = new 
        SimpleDateFormat("yyyy_MMdd_HHmmss").format(calendar.getTime());
        Utils.logi(TAG, "bootTimeStr = " + bootTimeStr);
        return bootTimeStr;
    }

    public static long getRebootTime() {
        long rebootTime = new Date().getTime() - SystemClock.elapsedRealtime();
        return new Date().getTime() - SystemClock.elapsedRealtime();
    }
//计时器 mTimeMillisecond 为你的开始时间
mTimeMillisecond = SystemClock.elapsedRealtime() / 1000;

private String calculateTimer() {
        if (mTimeMillisecond < 0) {
            Utils.logi(TAG, "System time adjust to past, just reset log time to 0,"
                    + " to avoid UI show incorrect");
            mTimeMillisecond = 0;
        }

        int hour = (int) mTimeMillisecond / 3600;
        if (hour > 9999) {
            Utils.loge(TAG, "There is something wrong with time record! The hour is " + hour);
            mTimeMillisecond = 0;
            Utils.loge(TAG, "There is something wrong with time record!");
        }
        int minute = (int) mTimeMillisecond / 60 % 60;
        long second = mTimeMillisecond % 60;
        String timerStr =
                "" + hour + ":" + (minute < 10 ? "0" : "") + minute + ":"
                        + (second < 10 ? "0" : "") + second;

        return timerStr;
    }

 

//获取手机当前时间   
 public  void getNowTime(){      
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      
        Date date = new Date(System.currentTimeMillis());      
        android.util.Log.i("zcailiu","onHotplug time ="+ simpleDateFormat.format(date));      
    }
//判断某service是否运行  
  public static boolean isServiceRunning(Context context, String className) {
        boolean isRunning = false;
        ActivityManager activityManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = null;
        try {
            serviceList = activityManager.getRunningServices(100);
        } catch (SecurityException re) {
            return false;
        }

        if (!(serviceList.size() > 0)) {
            return false;
        }

        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                isRunning = true;
                break;
            }
        }
        Utils.logi(TAG + "/Utils",
                "-->isServiceRunning() className = " + className + ", isRunning = " + isRunning);
        return isRunning;
    }
//java代码运行 adb 命令  并将返回命令的执行结果   
//alps\vendor\mediatek\proprietary\packages\apps\MTKLogger\src\com\mediatek\mtklogger\debugtool\AEEControlService.java

 private static StringBuffer systemexec(String command) {
        StringBuffer output = new StringBuffer();
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = new String();
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
            android.util.Log.i(TAG, output.toString());
            process.waitFor();
            reader.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            android.util.Log.i(TAG, "Operation failed.");
        } catch (InterruptedException ie) {
            ie.printStackTrace();
             android.util.Log.i(TAG, "Operation failed.");
        }
        return output;
    }
//将一个long类型的时间转换同成日常时间格式   
 /**
     * transfer long time to time string.
     * @param time long type
     * @return ex: 2012_1221_2359
     */
    public static String translateTime2(long time) {
        GregorianCalendar calendar = new GregorianCalendar();
        DecimalFormat df = new DecimalFormat();
        String pattern = "00";
        df.applyPattern(pattern);
        calendar.setTime(new Date(time));

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minu = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return "" + year + "_" + df.format(month) + df.format(day) + "_" + df.format(hour)
                + df.format(minu) + df.format(second);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值