手机当中肯定是有闹钟的,这是不用说的,要不然就不是手机了。对吧。
下面我根据广播的方式写了一个闹钟,大家请看图:

 

android闹钟——原代码_休闲

 

  1. 下面让我们看一下原代码的是如何写的, 
  2. package com.smart; 
  3.  
  4. import java.util.Calendar; 
  5.  
  6. import android.app.Activity; 
  7. import android.content.BroadcastReceiver; 
  8. import android.content.Context; 
  9. import android.content.Intent; 
  10. import android.content.SharedPreferences; 
  11. import android.media.MediaPlayer; 
  12.  
  13. public class AlarmReceiver extends BroadcastReceiver { 
  14. /** 
  15.  * 通过广播进行扫描,是否到达时间后再响起闹铃 
  16.  * 
  17.  * */ 
  18.     @Override 
  19.     public void onReceive(Context context, Intent intent) { 
  20.  
  21.         
  22.         SharedPreferences sharedPreferences=context.getSharedPreferences("alarm_record", Activity.MODE_PRIVATE); 
  23.         String hour=String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); 
  24.         String minute=String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)); 
  25.         String time=sharedPreferences.getString(hour+":"+minute, null);//小时与分, 
  26.         
  27.         if(time!=null){//判断是否为空,然后通过创建, 
  28.             MediaPlayer mediaPlayer=MediaPlayer.create(context, R.raw.llb); 
  29.             mediaPlayer.start();//开始 
  30.         } 
  31.     
  32.     } 
  33.  
  34. package com.smart; 
  35.  
  36. import android.app.Activity; 
  37. import android.app.AlarmManager; 
  38. import android.app.AlertDialog; 
  39. import android.app.PendingIntent; 
  40. import android.content.Context; 
  41. import android.content.DialogInterface; 
  42. import android.content.Intent; 
  43. import android.content.SharedPreferences; 
  44. import android.os.Bundle; 
  45. import android.view.View; 
  46. import android.view.View.OnClickListener; 
  47. import android.widget.Button; 
  48. import android.widget.TextView; 
  49. import android.widget.TimePicker; 
  50.  
  51. public class Main extends Activity implements OnClickListener{ 
  52.     
  53.     private TextView alarmRecord; 
  54.     private SharedPreferences sharedPreferences; 
  55.     
  56.     
  57.     @Override 
  58.     public void onCreate(Bundle savedInstanceState) { 
  59.         super.onCreate(savedInstanceState); 
  60.         setContentView(R.layout.main); 
  61.         Button addAlarm=(Button)findViewById(R.id.addAlarm); 
  62.         alarmRecord=(TextView)findViewById(R.id.alarmRecord); 
  63.         addAlarm.setOnClickListener(this); 
  64.         sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE); 
  65.         AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); 
  66.         Intent intent=new Intent(this,AlarmReceiver.class); 
  67.         PendingIntent pendingIntent=PendingIntent.getBroadcast(this0, intent, 0); 
  68.         aManager.setRepeating(AlarmManager.RTC, 060*1000, pendingIntent); 
  69.     
  70.     } 
  71.  
  72.     
  73.     @Override 
  74.     public void onClick(View v) { 
  75.         View view=getLayoutInflater().inflate(R.layout.llb,    null);// 
  76.         final TimePicker timePicker=(TimePicker)view.findViewById(R.id.timepicker); 
  77.         timePicker.setIs24HourView(true);// 
  78.         new AlertDialog.Builder(this).setTitle("设置闹铃时间").setView(view).setPositiveButton("确定"new DialogInterface.OnClickListener() { 
  79.             //设置标题 
  80.             @Override 
  81.             public void onClick(DialogInterface dialog, int which) { 
  82.             //按钮事件触发方法 
  83.                 String timeStr=String.valueOf(timePicker.getCurrentHour())+":"+String.valueOf(timePicker.getCurrentMinute()); 
  84.                 alarmRecord.setText(alarmRecord.getText().toString()+"\n"+timeStr); 
  85.                 sharedPreferences.edit().putString(timeStr, timeStr).commit(); 
  86.                 
  87.             } 
  88.         }).setNegativeButton("取消"null).show(); 
  89.         
  90.         
  91.         
  92.     
  93.         
  94.         
  95.         
  96.         
  97.     }