欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能

 更新時間:2019年06月15日 09:45:16   作者:清風丶徐來  
這篇文章主要為大家詳細介紹了Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android通過AlarmManager類實現(xiàn)鬧鐘,供大家參考,具體內容如下

簡介

鬧鐘是生活中最常用的功能了,很多App都可以加入該功能,提醒用戶某個時刻要做的事情。在Android系統(tǒng)中可以通過AlarmManager類實現(xiàn)鬧鐘,AlarmManager類是專門用來設定在某個指定的時間去完成指定的事件。AlarmManager提供了訪問系統(tǒng)警報的服務,只要在程序中設置了警報服務,AlarmManager就會通過onReceive()方法去還行這些事件,就算系統(tǒng)處于待機狀態(tài),同樣不會影響運行。可以通過Context.getSystemService方法來獲取該服務。接下來我們將使用AlarmManager來制作一個最簡單的鬧鐘。

讓我們來看一下AlarmManager都為我們提供了哪些方法,如下:

要實現(xiàn)鬧鐘,首先需要創(chuàng)建一個繼承自BroadcastReceiver的類,實現(xiàn)onReceive方法來接收這個Alarm服務,然后通過建立Intent和PendingIntent連接來調用Alarm組件,并通過TimePickerDialog來設置時間,當時間到我們指定的時間后onReceive方法接收到Alarm服務后即可進行提示。   

讓我們實現(xiàn)主界面布局,效果如下:

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

public class AlarmReceiver extends BroadcastReceiver {

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

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

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

接下來,在MainActivity中我們實現(xiàn)“設置鬧鐘”和“取消鬧鐘”的事件監(jiān)聽,讓我們來看一下具體實現(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來調用目標組件
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
        // 獲取鬧鐘管理的實例
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        // 設置鬧鐘
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        // 設置周期鬧鐘
        am.setRepeating(AlarmManager.RTC_WAKEUP,
          System.currentTimeMillis() + (10 * 1000),
          (24 * 60 * 60 * 1000), pendingIntent);
        String tmpS = "設置鬧鐘時間為" + 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);
    // 獲取鬧鐘管理實例
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    // 取消
    am.cancel(pendingIntent);
    info.setText("鬧鐘已經取消");
   }
  });
 }

 // 格式化字符串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這個類用于處理即將發(fā)生的事情,PendingIntent可以看作是對Intent的包裝,通常通過getActivity、getBroadcast、getService來得到PendingIntent的實例,當前Activity并不能馬上啟動它所包含的Intent,而是在外部執(zhí)行PendingIntent時,調用Intent。正是由于PendingIntent中保存有當前App的context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執(zhí)行PendingIntent里的Intent,就算在執(zhí)行時當前App已經不存在了,也能通過保存在PendingIntent里的Context照樣執(zhí)行Intent,另外還可以處理Intent執(zhí)行后的操作。常和AlarmManager和NotificationManager一起使用。

至此,一個簡單的功能就實現(xiàn)了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android Studio設置繪制布局時的視圖

    Android Studio設置繪制布局時的視圖

    這篇文章介紹了Android Studio設置繪制布局時視圖的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • Kotlin協(xié)程操作之創(chuàng)建啟動掛起恢復詳解

    Kotlin協(xié)程操作之創(chuàng)建啟動掛起恢復詳解

    本文的定位是協(xié)程的創(chuàng)建、啟動、掛起、恢復,也會示例一些簡單的使用,這里不對suspend講解,,也不對協(xié)程的高級用法做闡述(熱數(shù)據(jù)通道Channel、冷數(shù)據(jù)流Flow...),本文主要講協(xié)程稍微深入的全面知識
    2022-08-08
  • Android自定義ViewGroup之FlowLayout(三)

    Android自定義ViewGroup之FlowLayout(三)

    這篇文章主要為大家詳細介紹了Android自定義ViewGroup之FlowLayout,常用于關鍵字標簽,搜索熱詞列表等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 一款Android APK的結構構成解析

    一款Android APK的結構構成解析

    本篇文章介紹了我在學習過程中對于Android 程序的理解總結,刨析了apk的組成與產生過程,通讀本篇對大家的學習或工作具有一定的價值,需要的朋友可以參考下
    2021-10-10
  • Android Adapter的幾個常用方法

    Android Adapter的幾個常用方法

    這篇文章主要為大家詳細介紹了Android Adapter的幾個常用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • kotlin中EditText賦值Type mismatch方式

    kotlin中EditText賦值Type mismatch方式

    這篇文章主要介紹了kotlin中EditText賦值Type mismatch方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android 幀動畫使用詳情

    Android 幀動畫使用詳情

    這篇文章主要介紹本文介紹使用AnimationDrawable類來實現(xiàn)動畫效果,需要的朋友可以參考下文
    2021-08-08
  • Android開發(fā)事件處理的代碼如何寫手摸手教程

    Android開發(fā)事件處理的代碼如何寫手摸手教程

    這篇文章主要為大家介紹了Android開發(fā)事件處理的代碼如何寫手摸手教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • kotlin中使用ViewBinding綁定控件的方法

    kotlin中使用ViewBinding綁定控件的方法

    View Binding是Android Studio 3.6推出的新特性,主要用于減少findViewById的冗余代碼,但內部實現(xiàn)還是通過使用findViewById,這篇文章主要介紹了kotlin中使用ViewBinding綁定控件,需要的朋友可以參考下
    2024-03-03
  • android基于SwipeRefreshLayout實現(xiàn)類QQ的側滑刪除

    android基于SwipeRefreshLayout實現(xiàn)類QQ的側滑刪除

    本篇文章主要介紹了android基于SwipeRefreshLayout實現(xiàn)類QQ的側滑刪除,非常具有實用價值,需要的朋友可以參考下
    2017-10-10

最新評論