闹钟的设计(ClockDemo)

clock.java

package com.android.clock;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class Clock extends Activity {
    /** Called when the activity is first created. */
    private TextView tv = null;
	private Button btn_set = null;
    private Button btn_cel = null;
    private Calendar c = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        tv = (TextView)findViewById(R.id.textview);
        btn_set = (Button)findViewById(R.id.button1);
        btn_cel = (Button)findViewById(R.id.button2);
        c = Calendar.getInstance();
        btn_set.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				
				c.setTimeInMillis(System.currentTimeMillis());
				int hour = c.get(Calendar.HOUR_OF_DAY);
				int minute = c.get(Calendar.MINUTE);
				new TimePickerDialog(Clock.this, 
						new TimePickerDialog.OnTimeSetListener() {
							
							@Override
							public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
								
								c.setTimeInMillis(System.currentTimeMillis());
								c.set(Calendar.HOUR_OF_DAY, hourOfDay);
								c.set(Calendar.MINUTE, minute);
								c.set(Calendar.SECOND, 0);
								c.set(Calendar.MILLISECOND, 0);
								Intent intent = new Intent(Clock.this, AlarmReceiver.class);
								PendingIntent pi = PendingIntent.getBroadcast(Clock.this, 0, intent, 0);
								AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
								am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
								tv.setText("设置的闹钟时间为:" + hourOfDay + ":" + minute);
							}
						}, hour, minute, true).show();
			}});
        
        btn_cel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Clock.this, AlarmReceiver.class);
				PendingIntent pi = PendingIntent.getBroadcast(Clock.this, 0, intent, 0);
				AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
				am.cancel(pi);
				tv.setText("闹钟取消");
				
			}});
}
}

AlarmRecever.java

package com.android.clock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "闹钟时间到", Toast.LENGTH_LONG).show();
	}

}

main.xml








AndroidManifest.xml



    

    
        
            
                
                
            
        
		
    


你可能感兴趣的:(Android应用开发)