Android中怎么利用AlarmManager類實(shí)現(xiàn)鬧鐘功能-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)Android中怎么利用AlarmManager類實(shí)現(xiàn)鬧鐘功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

目前成都創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、鳳山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

實(shí)現(xiàn)接收Alarm服務(wù)的AlarmReceiver類,該類比較簡單,在收到消息后用一個(gè)Toast來提示用戶,具體實(shí)現(xiàn)代碼如下:


public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {  Toast.makeText(context, "您設(shè)置的時(shí)間到了!",   Toast.LENGTH_SHORT).show(); }}

由于使用了BroadcastReceiver,因此我們需要在AndroidManifest.xml文件中對其進(jìn)行聲明,如下:

<receiver android:name=".AlarmReceiver" android:process=":remote" />

接下來,在MainActivity中我們實(shí)現(xiàn)“設(shè)置鬧鐘”和“取消鬧鐘”的事件監(jiān)聽,讓我們來看一下具體實(shí)現(xiàn)代碼:

public class MainActivity extends Activity { private Button btnSet, btnCancel; private TextView info; private Calendar calendar; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  btnSet = (Button) findViewById(R.id.setalarm);  btnCancel = (Button) findViewById(R.id.cancelalarm);  info = (TextView) findViewById(R.id.info);  calendar = Calendar.getInstance();  btnSet.setOnClickListener(new OnClickListener() {   @Override   public void onClick(View v) {    // TODO Auto-generated method stub    calendar.setTimeInMillis(System.currentTimeMillis());    int mHour = calendar.get(Calendar.HOUR_OF_DAY);    int mMinute = calendar.get(Calendar.MINUTE);    new TimePickerDialog(MainActivity.this,      new TimePickerDialog.OnTimeSetListener() {       @Override       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和PendingIntent來調(diào)用目標(biāo)組件        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);        // 獲取鬧鐘管理的實(shí)例        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);        // 設(shè)置鬧鐘        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);        // 設(shè)置周期鬧鐘        am.setRepeating(AlarmManager.RTC_WAKEUP,          System.currentTimeMillis() + (10 * 1000),          (24 * 60 * 60 * 1000), pendingIntent);        String tmpS = "設(shè)置鬧鐘時(shí)間為" + format(hourOfDay)          + ":" + format(minute);        info.setText(tmpS);       }      }, mHour, mMinute, true).show();   }  });  btnCancel.setOnClickListener(new OnClickListener() {   @Override   public void onClick(View v) {    // TODO Auto-generated method stub    Intent intent = new Intent(MainActivity.this,      AlarmReceiver.class);    PendingIntent pendingIntent = PendingIntent.getBroadcast(      MainActivity.this, 0, intent, 0);    // 獲取鬧鐘管理實(shí)例    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);    // 取消    am.cancel(pendingIntent);    info.setText("鬧鐘已經(jīng)取消");   }  }); } // 格式化字符串7:3-->07:03 private String format(int x) {  String s = "" + x;  if (s.length() == 1) {   s = "0" + s;  }  return s; } @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.activity_main, menu);  return true; }}

在上述代碼中我們使用了PendingIntent,PendingIntent這個(gè)類用于處理即將發(fā)生的事情,PendingIntent可以看作是對Intent的包裝,通常通過getActivity、getBroadcast、getService來得到PendingIntent的實(shí)例,當(dāng)前Activity并不能馬上啟動(dòng)它所包含的Intent,而是在外部執(zhí)行PendingIntent時(shí),調(diào)用Intent。正是由于PendingIntent中保存有當(dāng)前App的context,使它賦予外部App一種能力,使得外部App可以如同當(dāng)前App一樣的執(zhí)行PendingIntent里的Intent,就算在執(zhí)行時(shí)當(dāng)前App已經(jīng)不存在了,也能通過保存在PendingIntent里的Context照樣執(zhí)行Intent,另外還可以處理Intent執(zhí)行后的操作。常和AlarmManager和NotificationManager一起使用。

看完上述內(nèi)容,你們對Android中怎么利用AlarmManager類實(shí)現(xiàn)鬧鐘功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站名稱:Android中怎么利用AlarmManager類實(shí)現(xiàn)鬧鐘功能-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://muchs.cn/article2/dddsoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、電子商務(wù)、網(wǎng)站導(dǎo)航網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)、小程序開發(fā)

廣告

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

網(wǎng)站托管運(yùn)營