闹钟设置

作者:王娇


       闹钟在生活中最常见了,在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;
    }
}


效果如下:

设置闹铃


闹钟设置_第1张图片


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


闹钟设置_第2张图片


取消闹铃:


闹钟设置_第3张图片

你可能感兴趣的:(android,String,service,calendar,Class,button)