最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能

 更新時(shí)間:2022年09月11日 10:52:26   作者:volador_r  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能的具體代碼,供大家參考,具體內(nèi)容如下

主要是通過(guò)廣播,實(shí)現(xiàn)一個(gè)鬧鐘的簡(jiǎn)易功能。

實(shí)現(xiàn)效果如下:

主界面為一個(gè)簡(jiǎn)易的設(shè)置鬧鐘Button,點(diǎn)擊“設(shè)置鬧鐘”彈出時(shí)間設(shè)置窗。設(shè)置成功后,會(huì)自動(dòng)彈出彈窗,提示“時(shí)間到了”。

打開(kāi)Android Studio,選擇File>New>New Project,選擇Phone and Tablet設(shè)備下的Empty Activity,創(chuàng)建項(xiàng)目名稱為“DrinkRemind”,并點(diǎn)擊“Finish”,完成項(xiàng)目創(chuàng)建。

首先打開(kāi)資源文件夾“res>layout>activity_main.xml”布局文件,創(chuàng)建主界面。這里我們需要一個(gè)設(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中,通過(guò)鬧鐘管理器 AlarmManager 來(lái)設(shè)定鬧鐘,通過(guò)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)時(shí)間 ? ? ? ?
?? ??? ?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(); ? ?//獲取日期對(duì)象 ? ? ? ? ??
?? ??? ??? ??? ?c.set(Calendar.HOUR_OF_DAY, hourOfDay); //設(shè)置鬧鐘小時(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)鬧鐘到達(dá)設(shè)定時(shí)間后,需要顯示鬧鐘的提醒框,這里新建AlarmActivity.java類(lèi),設(shè)置鬧鐘的提醒框,代碼如下:

public class AlarmActivity extends Activity { ? ?
?? ?@Override ? ?
?? ?public void onCreate(Bundle savedInstanceState) { ? ? ? ?
?? ??? ?super.onCreate(savedInstanceState); ? ? ? ?

?? ?//顯示鬧鐘提醒框 ? ? ? ?
?? ?new AlertDialog.Builder(AlarmActivity.this) ? ? ? ? ? ? ? ?
?? ??? ?.setTitle("鬧鐘") ? ? ? ? ? ? ? ?
?? ??? ?.setMessage("時(shí)間到了") ? ? ? ? ? ? ? ?
?? ??? ?.setPositiveButton("確定", new DialogInterface.OnClickListener(){ ? ? ? ? ? ? ? ? ? ?
?? ??? ??? ?public void onClick(DialogInterface dialogInterface, int which) {
?? ??? ??? ??? ?AlarmActivity.this.finish(); ? ? ? ? ? ? ? ? ? ?
?? ??? ??? ?} ? ? ? ? ? ? ? ?
?? ??? ?}).create().show(); ??
?? ?}
}

創(chuàng)建一個(gè)廣播接收類(lèi)AlarmReceiver,繼承BroadcastRecevice并實(shí)現(xiàn)OnReceive方法即可。當(dāng)廣播發(fā)送后,系統(tǒng)會(huì)去檢查廣播接收器的過(guò)濾器與廣播所發(fā)送的Intent是否一致, 如果一致則調(diào)用OnReceive方法。一旦接收到廣播,則會(huì)立即在OnReceive方法里調(diào)用AlarmActivity,顯示“時(shí)間到了”的彈窗。由于使用到了廣播機(jī)制,所以就算不開(kāi)著AlarmActivity,也可以在后臺(tái)監(jiān)控這個(gè)廣播。AlarmReceiver實(shí)現(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); ? ?
?? ?}
}

廣播類(lèi)AlarmReceiver和提醒框類(lèi)AlarmActivity需要在Manifest.xml中進(jìn)行配置,打開(kāi)AndroidMainfest.xml,在< application> 標(biāo)簽下增加配置。

進(jìn)行配置,打開(kāi)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>

基于以上代碼,我們就實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的Android端的鬧鐘提醒App,運(yùn)行后,點(diǎn)擊主界面的“設(shè)置鬧鐘”按鍵,效果如圖所示。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

鲁甸县| 壶关县| 株洲县| 辽源市| 淅川县| 铜陵市| 华蓥市| 黄平县| 沭阳县| 达尔| 乐业县| 赤壁市| 紫云| 怀宁县| 咸丰县| 自治县| 大同市| 门头沟区| 博乐市| 武安市| 台南县| 左权县| 象山县| 中方县| 驻马店市| 郑州市| 长沙市| 繁昌县| 饶河县| 囊谦县| 甘孜县| 眉山市| 勃利县| 蚌埠市| 青海省| 弥渡县| 大埔县| 安义县| 蓝田县| 江源县| 漯河市|