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

Android中怎么實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

成都創(chuàng)新互聯(lián)公司專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站制作、灌南網(wǎng)絡(luò)推廣、成都微信小程序、灌南網(wǎng)絡(luò)營(yíng)銷(xiāo)、灌南企業(yè)策劃、灌南品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供灌南建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):028-86922220,官方網(wǎng)址:muchs.cn

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());

看完上述內(nèi)容,你們掌握Android中怎么實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享題目:Android中怎么實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能
本文鏈接:http://muchs.cn/article18/gcesgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)做網(wǎng)站手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化