您好,登錄后才能下訂單哦!
不懂Android實現(xiàn)簡易鬧鐘功能的方法?其實想解決這個問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
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.通過代碼設(shè)置一個鬧鐘
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è)置鬧鐘的時間的Calendar對象
4.通過代碼取消一個鬧鐘:
/** * 取消鬧鐘 */ 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.鬧鐘響是彈出的對化框并播放音樂用AlarmActivity.java類實現(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ā)鬧鐘的時間(毫秒) 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); // 啟動一次只會執(zhí)行一次的鬧鐘 am.set(AlarmManager.RTC, triggerTime, op); // 指定時間重復(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è)定可通過上篇博文來獲取Calendar對象。
感謝你能夠認真閱讀完這篇文章,希望小編分享Android實現(xiàn)簡易鬧鐘功能的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學(xué)習(xí)!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。