Android實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能
本文實(shí)例為大家分享了Android通過(guò)廣播來(lái)實(shí)現(xiàn)鬧鐘的具體代碼,供大家參考,具體內(nèi)容如下
1.創(chuàng)建廣播接收RepeatingAlarm.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class RepeatingAlarm extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()!=null&&intent.getAction().equals("com.gcc.alarm")) {//自定義的action
intent = new Intent(context,AlarmActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
2.廣播在Manifest.xml中配置:
<receiver android:name=".RepeatingAlarm" > <intent-filter > <action android:name="com.gcc.alarm"/> </intent-filter> </receiver>
3.通過(guò)代碼設(shè)置一個(gè)鬧鐘
Intent intent = new Intent(this, RepeatingAlarm.class);
intent.setAction("com.gcc.alarm");
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC,
c.getTimeInMillis(), sender);//c為設(shè)置鬧鐘的時(shí)間的Calendar對(duì)象
4.通過(guò)代碼取消一個(gè)鬧鐘:
/**
* 取消鬧鐘
*/
private void cancleAlarm(){
Intent intent = new Intent(AlarmActivity.this,RepeatingAlarm.class);
intent.setAction("com.gcc.alarm");
PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);//取消鬧鐘
}
5.鬧鐘響是彈出的對(duì)化框并播放音樂(lè)用AlarmActivity.java類(lèi)實(shí)現(xiàn)
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
public class AlarmActivity extends Activity {
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_alarm);
mp = new MediaPlayer();
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try {
mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
file.getLength());
mp.prepare();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
mp.setVolume(0.5f, 0.5f);
mp.setLooping(true);
mp.start();
alarmOialog();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
}
}
public void alarmOialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("你有未處理的事件");
builder.setPositiveButton("稍后提醒",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alarm();
finish();
}
});
builder.setNegativeButton("停止", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cancleAlarm();
finish();// 關(guān)閉窗口
}
});
builder.show().setCanceledOnTouchOutside(false);
;
}
/**
* 取消鬧鐘
*/
private void cancleAlarm() {
// Create the same intent, and thus a matching IntentSender, for
// the one that was scheduled.
Intent intent = new Intent(AlarmActivity.this, RepeatingAlarm.class);
intent.setAction("com.gcc.alarm");
PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this,
0, intent, 0);
// And cancel the alarm.
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
}
private void alarm() {
// 獲取系統(tǒng)的鬧鐘服務(wù)
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// 觸發(fā)鬧鐘的時(shí)間(毫秒)
long triggerTime = System.currentTimeMillis() + 10000;
Intent intent = new Intent(this, RepeatingAlarm.class);
intent.setAction("com.gcc.alarm");
PendingIntent op = PendingIntent.getBroadcast(this, 0, intent, 0);
// 啟動(dòng)一次只會(huì)執(zhí)行一次的鬧鐘
am.set(AlarmManager.RTC, triggerTime, op);
// 指定時(shí)間重復(fù)執(zhí)行鬧鐘
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
}
6.注:
1.aty_alarm.xml為空布局,不需添加任何組件
2.使用MediaPlayer播放res/raw目錄下音頻文件的方法如下:
mp = new MediaPlayer();
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try {
mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
file.getLength());
7.功能不是很完善,需要的可以修改使用,鬧鐘時(shí)間設(shè)定可通過(guò)上篇博文來(lái)獲取Calendar對(duì)象。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過(guò)AlarmManager類(lèi)實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
- Android編程使用AlarmManager設(shè)置鬧鐘的方法
- Android 使用AlarmManager和NotificationManager來(lái)實(shí)現(xiàn)鬧鐘和通知欄
- Android手機(jī)鬧鐘服務(wù)AlarmManagerk開(kāi)發(fā)案例
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
- Android編程實(shí)現(xiàn)鬧鐘的方法詳解
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android使用AlarmManager設(shè)置鬧鐘功能
相關(guān)文章
Ubuntu Android源碼以及內(nèi)核下載與編譯
本文主要介紹Android源碼的下載和編譯,這里整理了相關(guān)資料及如何下載和編譯的詳細(xì)步驟,有需要的小伙伴可以參考下2016-09-09
Android入門(mén)之IntentService的使用教程詳解
IntentService的生命周期中有一個(gè)非常好的方法-onHandleIntent方法,它是一個(gè)abstract方法,開(kāi)發(fā)者在實(shí)現(xiàn)IntentService時(shí)可以覆蓋它來(lái)處理“長(zhǎng)事務(wù)”。本文就來(lái)聊聊IntentService的使用,需要的可以參考一下2022-12-12
Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開(kāi)或者關(guān)閉的實(shí)現(xiàn)代碼
本篇文章對(duì)Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開(kāi)或者關(guān)閉的實(shí)現(xiàn)用實(shí)例進(jìn)行了介紹。需要的朋友參考下2013-05-05
android中DatePicker和TimePicker的使用方法詳解
這篇文章主要介紹了android中DatePicker和TimePicker的使用方法,是Android中常用的功能,需要的朋友可以參考下2014-07-07
Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn)
Android 多屏互聯(lián)的時(shí)代,必然會(huì)出現(xiàn)多屏連接的問(wèn)題,本文主要介紹了Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Android Coil對(duì)比Glide深入分析探究
這篇文章主要介紹了Android Coil對(duì)比Glide,Coil是Android上的一個(gè)全新的圖片加載框架,它的全名叫做coroutine image loader,即協(xié)程圖片加載庫(kù)2023-02-02
5種Android數(shù)據(jù)存儲(chǔ)方式匯總
這篇文章主要為大家整理了5種Android數(shù)據(jù)存儲(chǔ)方式,列出了各存儲(chǔ)方式的優(yōu)缺點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

