android系统自带的振动器以及倒计时器
2016-04-06 09:38
429 查看
1.系统自带的振动器
private Vibrator mVibrator; ... // 使用系统服务检索振动器 mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); ... mVibrator.vibrate(200);// 振动0.2秒
1.系统自带的倒计时器
写CountDownTimerUtil,是android5.0底层CountDownTimer下面的源码。/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.tts.hotelmanager.utils; import android.os.Handler; import android.os.Message; import android.os.SystemClock; /** * Schedule a countdown until a time in the future, with * regular notifications on intervals along the way. * * Example of showing a 30 second countdown in a text field: * * <pre class="prettyprint"> * new CountDownTimer(30000, 1000) { * * public void onTick(long millisUntilFinished) { * mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); * } * * public void onFinish() { * mTextField.setText("done!"); * } * }.start(); * </pre> * * The calls to {@link #onTick(long)} are synchronized to this object so that * one call to {@link #onTick(long)} won't ever occur before the previous * callback is complete. This is only relevant when the implementation of * {@link #onTick(long)} takes an amount of time to execute that is significant * compared to the countdown interval. */ public abstract class CountDownTimerUtil { /** * Millis since epoch when alarm should stop. */ private final long mMillisInFuture; /** * The interval in millis that the user receives callbacks */ private final long mCountdownInterval; private long mStopTimeInFuture; /** * boolean representing if the timer was cancelled */ private boolean mCancelled = false; /** * @param millisInFuture The number of millis in the future from the call * to {@link #start()} until the countdown is done and {@link #onFinish()} * is called. * @param countDownInterval The interval along the way to receive * {@link #onTick(long)} callbacks. */ public CountDownTimerUtil(long millisInFuture, long countDownInterval) { mMillisInFuture = millisInFuture; mCountdownInterval = countDownInterval; } /** * Cancel the countdown. */ public synchronized final void cancel() { mCancelled = true; mHandler.removeMessages(MSG); } /** * Start the countdown. */ public synchronized final CountDownTimerUtil start() { mCancelled = false; if (mMillisInFuture <= 0) { onFinish(); return this; } mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture; mHandler.sendMessage(mHandler.obtainMessage(MSG)); return this; } /** * Callback fired on regular interval. * @param millisUntilFinished The amount of time until finished. */ public abstract void onTick(long millisUntilFinished); /** * Callback fired when the time is up. */ public abstract void onFinish(); private static final int MSG = 1; // handles counting down private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { synchronized (CountDownTimerUtil.this) { if (mCancelled) { return; } final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); if (millisLeft <= 0) { onFinish(); } else if (millisLeft < mCountdownInterval) { // no tick, just delay until done sendMessageDelayed(obtainMessage(MSG), millisLeft); } else { long lastTickStart = SystemClock.elapsedRealtime(); onTick(millisLeft); // take into account user's onTick taking time to execute long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); // special case: user's onTick took more than interval to // complete, skip to next interval while (delay < 0) delay += mCountdownInterval; sendMessageDelayed(obtainMessage(MSG), delay); } } } }; }
封装后的setCountDownTimerUtil() 方法
/** * 设计多次连接倒计时器 */ public static void setCountDownTimerUtil() { mCountDownTimerUtil = new CountDownTimerUtil(1000, 200) { /** * 每隔200毫秒之后进行的操作 */ @SuppressLint("InlinedApi") public void onTick(long millisUntilFinished) { mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); mAdapter = mBluetoothManager.getAdapter(); device = mAdapter.getRemoteDevice(mDeviceAddress); if (device != null) { // 取消扫描蓝牙设备 mAdapter.cancelDiscovery(); } mBluetoothGatt = device.connectGatt(mContext, false, myCallBack); } /** * 1000毫秒过去后进行的操作 */ public void onFinish() { // 取消倒计数器 mCountDownTimerUtil.cancel(); // // 取消连接周边设备 // disconnectRemoteDevice(mBluetoothGatt); } }; // 启动倒计时器 mCountDownTimerUtil.start(); }
相关文章推荐
- Android仿淘宝tab返回
- android:layout_weight的真实含义
- Android自动填充短信验证码
- 在 Android 下使用 FrameBuffer 绘图
- Android权限设置android.permission完整列表
- Android Error:Failed to install *.apk on device *: timeout Launch canceled!
- Please ensure that adb is correctly located at ‘F:\Android\adt-bund异常的解决之道
- Android中的缩略图
- Android属性动画,android属性
- Android之Intent
- Android Environment 常量含义
- Android系统配置数据库注释(settings.db)
- android 自定义控件
- Android深入浅出之Zygote
- Android 之 内存管理
- Android 之 zygote 与进程创建
- Android——列表视图 ListView(二)SimpleAdapter
- Android——列表视图 ListView(一)Arrayadapter
- Android 蓝牙4.0(BLE) 理解--紧随智能家居和可穿戴设备潮流
- Android Converty问题解决方案