Android編程鬧鐘設(shè)置方法詳解
本文實(shí)例講述了Android編程鬧鐘設(shè)置方法。分享給大家供大家參考,具體如下:
鬧鐘在生活中最常見了,在Android中可以通過AlarmManager來實(shí)現(xiàn)鬧鐘,AlarmManager類專門用來設(shè)置在某個(gè)指定的時(shí)間去完成指定的時(shí)間。AlarmManager就會通過onReceive()方法去執(zhí)行這些事件,就算系統(tǒng)處于待機(jī)狀態(tài),同樣不會影響運(yùn)行。可以通過Context.getSystemService方法來獲得該服務(wù)。AlarmManager中的方法不少,如下:
|
方法 |
說明 |
|
Cancel |
取消AlarmManager服務(wù) |
|
Set |
設(shè)置AlarmManager服務(wù) |
|
setInexactRepeating |
設(shè)置不精確周期 |
|
SetRepeating |
設(shè)置重復(fù)周期 |
|
setTimeZone |
設(shè)置時(shí)區(qū) |
要實(shí)現(xiàn)鬧鐘,首先需要?jiǎng)?chuàng)建一個(gè)繼承自BroadcastReceiver的類,實(shí)現(xiàn)onReceive方法來接受這個(gè)Alarm服務(wù),然后通過建立Intent和PendingIntent連接來調(diào)用Alarm組件。通過TimerPickerDialog來設(shè)置鬧鈴時(shí)間,當(dāng)時(shí)間到了我們指定的時(shí)間后onReceiver方法接受到Alarm服務(wù)后的界面。
首先實(shí)現(xiàn)接受Alarm服務(wù)的AlarmReceiver類,用Toast類提示用戶
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(arg0, "你設(shè)置的鬧鈴時(shí)間到了", Toast.LENGTH_LONG).show();
}
}
由于使用了BroadcastReceiver服務(wù),因此需要再AndroidManifest.xml中進(jìn)行聲明:
<receiver android:name=".AlarmReceiver" android:process=":remote"> </receiver>
然后需要設(shè)置鬧鈴和取消鬧鈴的時(shí)間進(jìn)行監(jiān)聽:
package cn.edu.pku;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
Button mButton1;
Button mButton2;
TextView mTextView;
Calendar calendar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
calendar=Calendar.getInstance();
mTextView=(TextView)findViewById(R.id.TextView01);
mButton1=(Button)findViewById(R.id.Button01);
mButton2=(Button)findViewById(R.id.Button02);
mButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
calendar.setTimeInMillis(System.currentTimeMillis());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000),
(24 * 60 * 60 * 1000), pendingIntent);
String tmps = "設(shè)置鬧鐘時(shí)間為" + format(hourOfDay) + ":" +format(minute);
mTextView.setText(tmps);
}
}, hour, minute, true).show();
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
mTextView.setText("鬧鈴已取消!");
}
});
}
private String format(int time){
String str = "" + time;
if(str.length() == 1){
str = "0" + str;
}
return str;
}
}
效果如下:
設(shè)置鬧鈴

現(xiàn)在時(shí)間到設(shè)置鬧鈴的時(shí)間:

取消鬧鈴:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android日期與時(shí)間操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- 簡單實(shí)現(xiàn)Android鬧鐘程序 附源碼
- Android編程實(shí)現(xiàn)鬧鐘的方法詳解
- Android實(shí)現(xiàn)簡易鬧鐘功能
- 簡單實(shí)現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android通過AlarmManager類實(shí)現(xiàn)簡單鬧鐘功能
- Android鬧鐘啟動時(shí)間設(shè)置無效問題的解決方法
- Android手機(jī)鬧鐘用法實(shí)例
- Android實(shí)現(xiàn)簡易的鬧鐘功能
相關(guān)文章
Android的OkHttp包處理用戶認(rèn)證的代碼實(shí)例分享
OkHttp包(GitHub主頁github.com/square/okhttp)是一款高人氣安卓HTTP支持包,這里我們來看一下Android的OkHttp包處理用戶認(rèn)證的代碼實(shí)例分享:2016-07-07
使用kotlin實(shí)現(xiàn)MVP的方式(簡單好用)
這篇文章主要介紹了使用kotlin實(shí)現(xiàn)MVP的方式(簡單好用),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android?源碼淺析RecyclerView?Adapter
這篇文章主要介紹了Android?源碼淺析之RecyclerView?Adapter示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android中ViewPager獲取當(dāng)前顯示的Fragment
這篇文章主要介紹了Android中ViewPager獲取當(dāng)前顯示的Fragment的兩種方法,一種是使用 getSupportFragmentManager().findFragmentByTag()方法,另一種是重寫適配器的 setPrimaryItem()方法,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
Android Canvas之drawBitmap方法案例詳解
這篇文章主要介紹了Android Canvas之drawBitmap方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android自定義View實(shí)現(xiàn)點(diǎn)贊控件
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)點(diǎn)贊控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

