最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程鬧鐘設(shè)置方法詳解

 更新時間:2016年10月29日 09:35:09   作者:pku_android  
這篇文章主要介紹了Android編程鬧鐘設(shè)置方法,結(jié)合實例形式分析了Android鬧鐘設(shè)置的步驟與時間監(jiān)聽的操作技巧,需要的朋友可以參考下

本文實例講述了Android編程鬧鐘設(shè)置方法。分享給大家供大家參考,具體如下:

鬧鐘在生活中最常見了,在Android中可以通過AlarmManager來實現(xiàn)鬧鐘,AlarmManager類專門用來設(shè)置在某個指定的時間去完成指定的時間。AlarmManager就會通過onReceive()方法去執(zhí)行這些事件,就算系統(tǒng)處于待機(jī)狀態(tài),同樣不會影響運行??梢酝ㄟ^Context.getSystemService方法來獲得該服務(wù)。AlarmManager中的方法不少,如下:

方法

說明

Cancel

取消AlarmManager服務(wù)

Set

設(shè)置AlarmManager服務(wù)

setInexactRepeating

設(shè)置不精確周期

SetRepeating

設(shè)置重復(fù)周期

setTimeZone

設(shè)置時區(qū)


要實現(xiàn)鬧鐘,首先需要創(chuàng)建一個繼承自BroadcastReceiver的類,實現(xiàn)onReceive方法來接受這個Alarm服務(wù),然后通過建立Intent和PendingIntent連接來調(diào)用Alarm組件。通過TimerPickerDialog來設(shè)置鬧鈴時間,當(dāng)時間到了我們指定的時間后onReceiver方法接受到Alarm服務(wù)后的界面。

首先實現(xiàn)接受Alarm服務(wù)的AlarmReceiver類,用Toast類提示用戶

public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    Toast.makeText(arg0, "你設(shè)置的鬧鈴時間到了", Toast.LENGTH_LONG).show();
  }
}

由于使用了BroadcastReceiver服務(wù),因此需要再AndroidManifest.xml中進(jìn)行聲明:

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

然后需要設(shè)置鬧鈴和取消鬧鈴的時間進(jìn)行監(jiān)聽:

package cn.edu.pku;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class AlarmActivity extends Activity {
  /** Called when the activity is first created. */
  Button mButton1;
  Button mButton2;
  TextView mTextView;
  Calendar calendar;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    calendar=Calendar.getInstance();
    mTextView=(TextView)findViewById(R.id.TextView01);
    mButton1=(Button)findViewById(R.id.Button01);
    mButton2=(Button)findViewById(R.id.Button02);
    mButton1.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        // TODO Auto-generated method stub
        calendar.setTimeInMillis(System.currentTimeMillis());
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        new TimePickerDialog(AlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
          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 intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000),
                (24 * 60 * 60 * 1000), pendingIntent);
            String tmps = "設(shè)置鬧鐘時間為" + format(hourOfDay) + ":" +format(minute);
            mTextView.setText(tmps);
          }
        }, hour, minute, true).show();
      }
    });
    mButton2.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        mTextView.setText("鬧鈴已取消!");
      }
    });
  }
  private String format(int time){
    String str = "" + time;
    if(str.length() == 1){
      str = "0" + str;
    }
    return str;
  }
}

效果如下:

設(shè)置鬧鈴

現(xiàn)在時間到設(shè)置鬧鈴的時間:

取消鬧鈴:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android日期與時間操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android 8.0不能自動安裝APK問題的解決方法(完美適配)

    Android 8.0不能自動安裝APK問題的解決方法(完美適配)

    這篇文章主要給大家介紹了關(guān)于Android 8.0不能自動安裝APK問題的解決方法(完美適配),這里的自動安裝是指下載完成后,自動彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • 基于Android自定義控件實現(xiàn)刮刮樂效果

    基于Android自定義控件實現(xiàn)刮刮樂效果

    這篇文章主要介紹了基于Android自定義控件實現(xiàn)刮刮樂效果 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android購物車項目快速開發(fā)

    Android購物車項目快速開發(fā)

    這篇文章主要為大家詳細(xì)介紹了Android購物車項目快速開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android 實現(xiàn)滑動方法總結(jié)

    Android 實現(xiàn)滑動方法總結(jié)

    這篇文章主要介紹了Android 實現(xiàn)滑動方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android實現(xiàn)button居中的方法

    Android實現(xiàn)button居中的方法

    這篇文章主要介紹了Android實現(xiàn)button居中的方法,涉及Android的XML布局技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Android編程之菜單實現(xiàn)方法

    Android編程之菜單實現(xiàn)方法

    這篇文章主要介紹了Android編程之菜單實現(xiàn)方法,以實例形式較為詳細(xì)的分析了Android編程實現(xiàn)菜單的布局及功能相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android實現(xiàn)微信支付功能詳解

    Android實現(xiàn)微信支付功能詳解

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)微信支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android使用Notification實現(xiàn)通知功能

    Android使用Notification實現(xiàn)通知功能

    這篇文章主要為大家詳細(xì)介紹了Android使用Notification實現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 深入了解Android?IO的底層原理

    深入了解Android?IO的底層原理

    這篇文章主要介紹了深入了解Android?IO的底層原理,IO有緩沖與非緩沖?IO、直接與非直接?IO、阻塞與非阻塞?IO、同步與異步?IO等分類,具體詳情感興趣的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)

    Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)

    這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論

贡觉县| 开封县| 根河市| 保康县| 炎陵县| 玛纳斯县| 冀州市| 公主岭市| 闽侯县| 余庆县| 巫溪县| 阿瓦提县| 庄河市| 慈溪市| 葫芦岛市| 山阳县| 台南市| 铜川市| 灵台县| 临洮县| 化州市| 六盘水市| 玉溪市| 德庆县| 石狮市| 高邑县| 高陵县| 湖北省| 屏南县| 青田县| 叶城县| 荔波县| 江安县| 榆社县| 贡觉县| 康平县| 纳雍县| 穆棱市| 台南市| 台前县| 鞍山市|