基于Alarmmanager實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
本文實(shí)例為大家分享了Alarmmanager實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啟動(dòng)鬧鐘"
android:id="@+id/button"
android:onClick="startAlarmClick"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設(shè)置鬧鐘"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:onClick="startSetAlarmClick"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
MainActivity.java
package com.example.haige.alarmmanager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*
啟動(dòng)鬧鐘
*/
public void startAlarmClick(View view)
{
//獲取系統(tǒng)的鬧鐘服務(wù)
AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//觸發(fā)鬧鐘的時(shí)間(毫秒)
long triggerTime= System.currentTimeMillis()+3000;
Intent intent=new Intent(this,Alarmctivity.class);
PendingIntent op=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//啟動(dòng)一次只會(huì)執(zhí)行一次的鬧鐘
am.set(AlarmManager.RTC,triggerTime,op);
// //指定時(shí)間重復(fù)執(zhí)行鬧鐘
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
/*
設(shè)置鬧鐘
*/
public void startSetAlarmClick(View view)
{
//獲取系統(tǒng)的鬧鐘服務(wù)
AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//觸發(fā)鬧鐘的時(shí)間(毫秒)
long triggerTime= System.currentTimeMillis()+3000;
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent op=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//啟動(dòng)一次只會(huì)執(zhí)行一次的鬧鐘
am.set(AlarmManager.RTC,triggerTime,op);
// //指定時(shí)間重復(fù)執(zhí)行鬧鐘
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
}
activity_alarmctivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.haige.alarmmanager.Alarmctivity"> </RelativeLayout>
Alarmctivity.java
package com.example.haige.alarmmanager;
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.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import java.io.IOException;
public class Alarmctivity extends Activity
{
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarmctivity);
mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.parse("/storage/sdcard1/kugou/聽力/南拳媽媽-你不像她.mp3"));
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
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) {
finish();//關(guān)閉窗口
}
});
builder.show();
}
private void alarm() {
//獲取系統(tǒng)的鬧鐘服務(wù)
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//觸發(fā)鬧鐘的時(shí)間(毫秒)
long triggerTime = System.currentTimeMillis() + 5000;
Intent intent = new Intent(this, Alarmctivity.class);
PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//啟動(dòng)一次只會(huì)執(zhí)行一次的鬧鐘
am.set(AlarmManager.RTC, triggerTime, op);
//指定時(shí)間重復(fù)執(zhí)行鬧鐘
// am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
}
廣播接收器AlarmReceiver.java
package com.example.haige.alarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"起床啦,起床啦!",Toast.LENGTH_SHORT).show();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android可循環(huán)顯示圖像的Android Gallery組件用法實(shí)例
這篇文章主要介紹了Android可循環(huán)顯示圖像的Android Gallery組件用法,結(jié)合實(shí)例形式分析了Gallery組件的功能,使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-04-04
深入學(xué)習(xí)Kotlin?枚舉的簡(jiǎn)潔又高效進(jìn)階用法
這篇文章主要為大家介紹了深入學(xué)習(xí)Kotlin?枚舉簡(jiǎn)潔又高效的進(jìn)階用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android軟件啟動(dòng)動(dòng)畫及動(dòng)畫結(jié)束后跳轉(zhuǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android軟件啟動(dòng)動(dòng)畫及動(dòng)畫結(jié)束后跳轉(zhuǎn)的實(shí)現(xiàn)方法,實(shí)例分析了Android圖片播放及定時(shí)器的相關(guān)使用技巧,非常具有使用價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android編程仿Iphone拖動(dòng)相片特效Gallery的簡(jiǎn)單應(yīng)用示例
這篇文章主要介紹了Android編程仿Iphone拖動(dòng)相片特效Gallery的簡(jiǎn)單應(yīng)用,結(jié)合實(shí)例形式分析了Android圖形拖動(dòng)特效的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android簡(jiǎn)單封裝一個(gè)MVP基類流程詳解
MVP是從經(jīng)典的模式MVC演變而來,它們的基本思想有相通的地方:Controller/Presenter負(fù)責(zé)邏輯的處理,Model提供數(shù)據(jù),View負(fù)責(zé)顯示。下面這篇文章主要給大家介紹了關(guān)于Android從實(shí)現(xiàn)到封裝MVP的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧2023-03-03
Android利用屬性動(dòng)畫實(shí)現(xiàn)優(yōu)酷菜單
這篇文章主要為大家詳細(xì)介紹了Android利用屬性動(dòng)畫實(shí)現(xiàn)優(yōu)酷菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android ViewPager實(shí)現(xiàn)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)輪播圖效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

