Android基础知识

1、查询某个服务是否存在
private boolean isServiceWork(Context context, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List myList = myAM.getRunningServices(150);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }


        Log.d(TAG, "isServiceWork="+isWork);
        return isWork;

    }

/**
     *
     * 判断MainActivity是否处于栈顶
     * @return  true在栈顶false不在栈顶
     */
    private boolean isMainActivityTop( ){
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        return name.equals(MainActivity.class.getName());
    }

//HOME键监听
    private BroadcastReceiver homePressReceiver = new BroadcastReceiver() {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                System.out.println("reason : " + reason);
                if(reason!=null&&reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)){
                    System.out.println("按HOME键使应用进入了后台");
                    mWindowView.setVisibility(View.GONE);
                }
            }
        }
    };



/**
* 显示时:分:秒
* ① ②
*/

private String stringForDurationOptimized(final long millis) {
        final int totalSeconds = (int) millis / 1000;
        final int seconds = totalSeconds % 60;
        final int minutes = (totalSeconds / 60) % 60;
        final int hours = totalSeconds / 3600;
        //optimize format time, but not support special language
        final StringBuilder builder = new StringBuilder(10);
        if (hours > 0) {
            builder.append(hours)
                    .append(":");
        }
        if (minutes < 10) {
            builder.append("0");
        }
        builder.append(minutes);
        builder.append(":");
        if (seconds < 10) {
            builder.append("0");
        }
        builder.append(seconds);
        return builder.toString();
    }


private String formatLongToTimeStr(Long l) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l.intValue();
        if (second > 60) {
            minute = second / 60;         //取整
            second = second % 60;         //取余
        }


        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }


        String strtime = (hour >= 10 ? hour : ("0" + hour)) + ":" + (minute >= 10 ? minute : ("0" + minute)) + ":" + (second >= 10 ? second : ("0" + second));
        return strtime;


    }



距离下一个小时的时间
 private long countDownTime() {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        String time = df.format(day);
        String[] sp = time.split(":");
        int hour = Integer.parseInt(sp[0]);
        int minute = Integer.parseInt(sp[1]);
        int second = Integer.parseInt(sp[2]);
        long difference = (hour + 1) * 60 * 60 - (hour * 60 * 60 + minute * 60 + second);
        System.out.println(difference);
        return difference;
    }

你可能感兴趣的:(android基础知识)