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

 

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. } 
  35. package com.smart; 
  36.  
  37. import android.app.Activity; 
  38. import android.app.AlarmManager; 
  39. import android.app.AlertDialog; 
  40. import android.app.PendingIntent; 
  41. import android.content.Context; 
  42. import android.content.DialogInterface; 
  43. import android.content.Intent; 
  44. import android.content.SharedPreferences; 
  45. import android.os.Bundle; 
  46. import android.view.View; 
  47. import android.view.View.OnClickListener; 
  48. import android.widget.Button; 
  49. import android.widget.TextView; 
  50. import android.widget.TimePicker; 
  51.  
  52. public class Main extends Activity implements OnClickListener{ 
  53.     
  54.     private TextView alarmRecord; 
  55.     private SharedPreferences sharedPreferences; 
  56.     
  57.     
  58.     @Override 
  59.     public void onCreate(Bundle savedInstanceState) { 
  60.         super.onCreate(savedInstanceState); 
  61.         setContentView(R.layout.main); 
  62.         Button addAlarm=(Button)findViewById(R.id.addAlarm); 
  63.         alarmRecord=(TextView)findViewById(R.id.alarmRecord); 
  64.         addAlarm.setOnClickListener(this); 
  65.         sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE); 
  66.         AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); 
  67.         Intent intent=new Intent(this,AlarmReceiver.class); 
  68.         PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, intent, 0); 
  69.         aManager.setRepeating(AlarmManager.RTC, 0, 60*1000, pendingIntent); 
  70.     
  71.     } 
  72.  
  73.     
  74.     @Override 
  75.     public void onClick(View v) { 
  76.         View view=getLayoutInflater().inflate(R.layout.llb,    null);// 
  77.         final TimePicker timePicker=(TimePicker)view.findViewById(R.id.timepicker); 
  78.         timePicker.setIs24HourView(true);// 
  79.         new AlertDialog.Builder(this).setTitle("设置闹铃时间").setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  80.             //设置标题 
  81.             @Override 
  82.             public void onClick(DialogInterface dialog, int which) { 
  83.             //按钮事件触发方法 
  84.                 String timeStr=String.valueOf(timePicker.getCurrentHour())+":"+String.valueOf(timePicker.getCurrentMinute()); 
  85.                 alarmRecord.setText(alarmRecord.getText().toString()+"\n"+timeStr); 
  86.                 sharedPreferences.edit().putString(timeStr, timeStr).commit(); 
  87.                 
  88.             } 
  89.         }).setNegativeButton("取消", null).show(); 
  90.         
  91.         
  92.         
  93.     
  94.         
  95.         
  96.         
  97.         
  98.     } 
  99. } 

 


本文转自 llb988 51CTO博客,原文链接:http://blog.51cto.com/llb988/515862,如需转载请自行联系原作者

相关文章:

  • 2021-12-06
  • 2021-12-17
  • 2021-12-01
  • 2021-12-06
  • 2022-02-24
  • 2022-12-23
  • 2021-05-26
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案