亲宝软件园·资讯

展开

Android闹钟设置方法 Android编程闹钟设置方法详解

pku_android 人气:2
想了解Android编程闹钟设置方法详解的相关内容吗,pku_android在本文为您仔细讲解Android闹钟设置方法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,闹钟,设置方法,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论