Android實現(xiàn)簡易的鬧鐘功能
本文實例為大家分享了Android實現(xiàn)簡易的鬧鐘功能的具體代碼,供大家參考,具體內(nèi)容如下
主要是通過廣播,實現(xiàn)一個鬧鐘的簡易功能。
實現(xiàn)效果如下:
主界面為一個簡易的設(shè)置鬧鐘Button,點擊“設(shè)置鬧鐘”彈出時間設(shè)置窗。設(shè)置成功后,會自動彈出彈窗,提示“時間到了”。

打開Android Studio,選擇File>New>New Project,選擇Phone and Tablet設(shè)備下的Empty Activity,創(chuàng)建項目名稱為“DrinkRemind”,并點擊“Finish”,完成項目創(chuàng)建。
首先打開資源文件夾“res>layout>activity_main.xml”布局文件,創(chuàng)建主界面。這里我們需要一個設(shè)置鬧鐘的button,修改activity_main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ?? ?android:orientation="vertical" ? ? ?? ?android:layout_width="fill_parent" ? ? ?? ?android:layout_height="fill_parent" ? ? ?? ?xmlns:app="http://schemas.android.com/apk/res-auto" ? ? ?? ?xmlns:tools="http://schemas.android.com/tools" ? ? ?? ?tools:context=".MainActivity"> ? ?? ? ? ?? ?<Button ? ? ? ? ?? ??? ?android:id="@+id/set_clock" ? ? ? ? ?? ??? ?android:layout_width="fill_parent" ? ? ? ? ?? ??? ?android:layout_height="wrap_content" ? ? ? ? ?? ??? ?android:text="設(shè)置鬧鐘"/> </LinearLayout>
在MainActivity.java中,通過鬧鐘管理器 AlarmManager 來設(shè)定鬧鐘,通過TimePickerDialog 彈出鬧鐘設(shè)置窗口。修改MainActivity.java中的代碼如下:
public class MainActivity extends Activity { ? ?
?? ?private Button btn; ? ?
?? ?private AlarmManager alarmManager; ?//鬧鐘管理器 ? ?
?? ?
?? ?@Override ? ?
?? ?protected void onCreate(Bundle savedInstanceState) { ? ? ? ?
?? ??? ?super.onCreate(savedInstanceState); ? ? ? ?
?? ??? ?setContentView(R.layout.activity_main); ? ? ? ?
?? ??? ?//獲取鬧鐘管理器 ? ? ? ?
?? ??? ?alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); ? ? ? ?
?? ??? ?btn = (Button)findViewById(R.id.set_clock); ? ? ? ?
?? ??? ?btn.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ?
?? ??? ??? ?@Override ? ? ? ? ? ?
?? ??? ??? ?public void onClick(View view) { ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?setClock(view); ? ? ? ? ? ?
?? ??? ??? ?} ? ? ? ?
?? ??? ?}); ??
?? ?} ? ?
?? ?public void setClock(View view){ ? ? ? ?
?? ??? ?//獲取當(dāng)前系統(tǒng)時間 ? ? ? ?
?? ??? ?Calendar calendar = Calendar.getInstance(); ? ? ? ?
?? ??? ?int hour = calendar.get(Calendar.HOUR_OF_DAY); ? ? ? ?
?? ??? ?int minute = calendar.get(Calendar.MINUTE); ? ? ? ?
?? ??? ?//彈出鬧鐘框 ? ? ? ?
?? ??? ?TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { ? ? ? ? ? ?
?? ??? ??? ?@Override ? ? ? ? ? ?
?? ??? ??? ?public void onTimeSet(TimePicker view, int hourOfDay, int minute) { ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?Calendar c = Calendar.getInstance(); ? ?//獲取日期對象 ? ? ? ? ??
?? ??? ??? ??? ?c.set(Calendar.HOUR_OF_DAY, hourOfDay); //設(shè)置鬧鐘小時數(shù) ? ? ? ? ? ? ?
?? ??? ??? ??? ?c.set(Calendar.MINUTE, minute); //設(shè)置鬧鐘分鐘數(shù) ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?//創(chuàng)建pendingIntent ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0X102, intent,0); ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?//設(shè)置鬧鐘 ? ? ? ? ? ? ? ?
?? ??? ??? ??? ?alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
?? ??? ??? ??? ?Toast.makeText(MainActivity.this, "鬧鐘設(shè)置成功", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ?
?? ??? ??? ?} ? ? ? ?
?? ??? ?},hour,minute,true); ? ? ? ?
?? ??? ?timePickerDialog.show(); ? ?
?? ?}
}當(dāng)鬧鐘到達設(shè)定時間后,需要顯示鬧鐘的提醒框,這里新建AlarmActivity.java類,設(shè)置鬧鐘的提醒框,代碼如下:
public class AlarmActivity extends Activity { ? ?
?? ?@Override ? ?
?? ?public void onCreate(Bundle savedInstanceState) { ? ? ? ?
?? ??? ?super.onCreate(savedInstanceState); ? ? ? ?
?? ?//顯示鬧鐘提醒框 ? ? ? ?
?? ?new AlertDialog.Builder(AlarmActivity.this) ? ? ? ? ? ? ? ?
?? ??? ?.setTitle("鬧鐘") ? ? ? ? ? ? ? ?
?? ??? ?.setMessage("時間到了") ? ? ? ? ? ? ? ?
?? ??? ?.setPositiveButton("確定", new DialogInterface.OnClickListener(){ ? ? ? ? ? ? ? ? ? ?
?? ??? ??? ?public void onClick(DialogInterface dialogInterface, int which) {
?? ??? ??? ??? ?AlarmActivity.this.finish(); ? ? ? ? ? ? ? ? ? ?
?? ??? ??? ?} ? ? ? ? ? ? ? ?
?? ??? ?}).create().show(); ??
?? ?}
}創(chuàng)建一個廣播接收類AlarmReceiver,繼承BroadcastRecevice并實現(xiàn)OnReceive方法即可。當(dāng)廣播發(fā)送后,系統(tǒng)會去檢查廣播接收器的過濾器與廣播所發(fā)送的Intent是否一致, 如果一致則調(diào)用OnReceive方法。一旦接收到廣播,則會立即在OnReceive方法里調(diào)用AlarmActivity,顯示“時間到了”的彈窗。由于使用到了廣播機制,所以就算不開著AlarmActivity,也可以在后臺監(jiān)控這個廣播。AlarmReceiver實現(xiàn)代碼如下:
public class AlarmReceiver extends BroadcastReceiver {?
? ?
?? ?@Override ? ?
?? ?public void onReceive(Context context, Intent intent) { ? ? ? ?
?? ??? ?Intent i = new Intent(context, AlarmActivity.class);
?? ??? ?i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ? ? ? ?
?? ??? ?context.startActivity(i); ? ?
?? ?}
}廣播類AlarmReceiver和提醒框類AlarmActivity需要在Manifest.xml中進行配置,打開AndroidMainfest.xml,在< application> 標(biāo)簽下增加配置。
進行配置,打開AndroidMainfest.xml,在<application>標(biāo)簽下增加配置。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ?? ?package="com.example.drinkremind"> ? ? ?? ?<application ? ? ? ? ?? ??? ?android:allowBackup="true" ? ? ? ? ?? ??? ?...... ? ? ? ? ?? ??? ?<activity android:name=".MainActivity"> ? ? ? ? ? ? ?? ??? ??? ?<intent-filter> ? ? ? ? ? ? ? ? ?? ??? ??? ??? ?<action android:name="android.intent.action.MAIN" /> ? ? ? ? ? ? ? ? ?? ??? ??? ??? ?<category android:name="android.intent.category.LAUNCHER" /> ? ? ? ? ? ? ?? ??? ??? ?</intent-filter> ? ? ? ? ?? ??? ?</activity> ? ? ? ? ?? ??? ?<activity android:name=".AlarmActivity"/> ? ? ? ? ?? ??? ?<receiver android:name=".AlarmReceiver" android:process=":remote"/> ? ? ?? ?</application> </manifest>
基于以上代碼,我們就實現(xiàn)了一個簡單的Android端的鬧鐘提醒App,運行后,點擊主界面的“設(shè)置鬧鐘”按鍵,效果如圖所示。

完成鬧鐘設(shè)置,等待至到達鬧鐘設(shè)置時間后,界面會自動彈出“時間到了”的提醒框,如圖所示。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android定時器Timer的停止和重啟實現(xiàn)代碼
本篇文章主要介紹了Android實現(xiàn)定時器Timer的停止和重啟實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter?DateTime日期轉(zhuǎn)換的詳細使用
本文主要介紹了Flutter?DateTime日期轉(zhuǎn)換的詳細使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
android基本控件ToggleButton&Switch使用指南
本文給大家匯總介紹了android的2個基本控件ToggleButton和Switch的使用方法,非常的詳細,有需要的小伙伴可以參考下。2016-01-01
Android List刪除重復(fù)數(shù)據(jù)
這篇文章主要介紹了Android List刪除重復(fù)數(shù)據(jù)的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06

