首页 > 系统 > Android > 正文

Android编程闹钟设置方法详解

2019-12-12 04:53:33
字体:
来源:转载
供稿:网友

本文实例讲述了Android编程闹钟设置方法。分享给大家供大家参考,具体如下:

闹钟在生活中最常见了,在Android中可以通过AlarmManager来实现闹钟,AlarmManager类专门用来设置在某个指定的时间去完成指定的时间。AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行。可以通过Context.getSystemService方法来获得该服务。AlarmManager中的方法不少,如下:

方法

说明

Cancel

取消AlarmManager服务

Set

设置AlarmManager服务

setInexactRepeating

设置不精确周期

SetRepeating

设置重复周期

setTimeZone

设置时区


要实现闹钟,首先需要创建一个继承自BroadcastReceiver的类,实现onReceive方法来接受这个Alarm服务,然后通过建立Intent和PendingIntent连接来调用Alarm组件。通过TimerPickerDialog来设置闹铃时间,当时间到了我们指定的时间后onReceiver方法接受到Alarm服务后的界面。

首先实现接受Alarm服务的AlarmReceiver类,用Toast类提示用户

public class AlarmReceiver extends BroadcastReceiver {  @Override  public void onReceive(Context arg0, Intent arg1) {    // TODO Auto-generated method stub    Toast.makeText(arg0, "你设置的闹铃时间到了", Toast.LENGTH_LONG).show();  }}

由于使用了BroadcastReceiver服务,因此需要再AndroidManifest.xml中进行声明:

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

然后需要设置闹铃和取消闹铃的时间进行监听:

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 = "设置闹钟时间为" + 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;  }}

效果如下:

设置闹铃

现在时间到设置闹铃的时间:

取消闹铃:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android日期与时间操作技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表