Android 闹钟,实现demo
生活随笔
收集整理的這篇文章主要介紹了
Android 闹钟,实现demo
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
可變時(shí)間格式:"yyyy-MM-dd HH:mm:ss"
保存鬧鐘時(shí)間:
-
SimpleDateFormat df = new SimpleDateFormat("HH:mm");//設(shè)置日期格式
-
String thistime = df.format(new Date());
-
SharedPreferences sharedPreferences = getSharedPreferences("industryInfo", Context.MODE_PRIVATE); //私有數(shù)據(jù)
-
SharedPreferences.Editor editor = sharedPreferences.edit();//獲取編輯器
-
editor.putString(“retime”, thistime);//名稱 id
-
editor.commit();//提交
?
監(jiān)聽時(shí)間變化:
監(jiān)聽service
/*** 作者:created by meixi* 郵箱:13164716840@163.com* 日期:2018/9/27 09*/ public class Serview extends Service {/*** 廣播接受者*/private BroadcastReceiver mBatInfoReceiver;private String TAG = "lgq--------------------";@Overridepublic void onCreate() {super.onCreate();initBroadcastReceiver();}/*** 注冊(cè)廣播*/private void initBroadcastReceiver() {final IntentFilter filter = new IntentFilter();// 屏幕滅屏廣播filter.addAction(Intent.ACTION_SCREEN_OFF);//關(guān)機(jī)廣播filter.addAction(Intent.ACTION_SHUTDOWN);// 屏幕亮屏廣播filter.addAction(Intent.ACTION_SCREEN_ON);// 屏幕解鎖廣播 // filter.addAction(Intent.ACTION_USER_PRESENT);// 當(dāng)長(zhǎng)按電源鍵彈出“關(guān)機(jī)”對(duì)話或者鎖屏?xí)r系統(tǒng)會(huì)發(fā)出這個(gè)廣播// example:有時(shí)候會(huì)用到系統(tǒng)對(duì)話框,權(quán)限可能很高,會(huì)覆蓋在鎖屏界面或者“關(guān)機(jī)”對(duì)話框之上,// 所以監(jiān)聽這個(gè)廣播,當(dāng)收到時(shí)就隱藏自己的對(duì)話,如點(diǎn)擊pad右下角部分彈出的對(duì)話框filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//監(jiān)聽日期變化filter.addAction(Intent.ACTION_DATE_CHANGED);filter.addAction(Intent.ACTION_TIME_CHANGED);filter.addAction(Intent.ACTION_TIME_TICK);mBatInfoReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(final Context context, final Intent intent) {String action = intent.getAction();if (Intent.ACTION_SCREEN_ON.equals(action)) {Log.i(TAG, "screen on");} else if (Intent.ACTION_TIME_TICK.equals(action)) {//日期變化步數(shù)重置為0Log.i("lgq0000000000000000","日期變化步數(shù)重置為0===="+action);SharedPreferences share = getSharedPreferences("industryInfo", Activity.MODE_PRIVATE);String industryOne = share.getString("retime", "");//名稱 獲取idif (industryOne.equals(new SimpleDateFormat("HH:mm").format(new Date()))){//todo 鬧鐘到了}}}};registerReceiver(mBatInfoReceiver, filter);}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;} } <service android:name=".testt.Serview"><intent-filter><!-- 系統(tǒng)啟動(dòng)完成后會(huì)調(diào)用--><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.DATE_CHANGED" /><action android:name="android.intent.action.MEDIA_MOUNTED" /><action android:name="android.intent.action.USER_PRESENT" /><action android:name="android.intent.action.ACTION_TIME_TICK" /><action android:name="android.intent.action.ACTION_POWER_CONNECTED" /><action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /></intent-filter> </service>?
/*** 開啟計(jì)步服務(wù)*/private void setupService() {Intent intent = new Intent(this, Serview.class); // isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);startService(intent);}?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Android 闹钟,实现demo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SCRT804安装教程
- 下一篇: OkHttp 3.x 源码解析之Inte