EX06_10.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:8k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex06_10;
  2. /* import相关class */
  3. import java.util.Calendar;
  4. import android.app.Activity;
  5. import android.app.AlarmManager;
  6. import android.app.AlertDialog;
  7. import android.app.PendingIntent;
  8. import android.app.TimePickerDialog;
  9. import android.content.DialogInterface;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.widget.TimePicker;
  18. import android.widget.Toast;
  19. public class EX06_10 extends Activity
  20. {
  21.   /* 声明对象变量 */
  22.   TextView setTime1;
  23.   TextView setTime2;
  24.   Button mButton1;
  25.   Button mButton2;
  26.   Button mButton3;
  27.   Button mButton4;
  28.   Calendar c=Calendar.getInstance();
  29.   @Override
  30.   public void onCreate(Bundle savedInstanceState)
  31.   {
  32.     super.onCreate(savedInstanceState);
  33.     /* 载入main.xml Layout */
  34.     setContentView(R.layout.main);
  35.     /* 以下为只响一次的闹钟的设置 */         
  36.     setTime1=(TextView) findViewById(R.id.setTime1);
  37.     /* 只响一次的闹钟的设置Button */
  38.     mButton1=(Button)findViewById(R.id.mButton1);
  39.     mButton1.setOnClickListener(new View.OnClickListener()
  40.     {
  41.       public void onClick(View v)
  42.       {
  43.         /* 取得点击按钮时的时间作为TimePickerDialog的默认值 */
  44.         c.setTimeInMillis(System.currentTimeMillis());
  45.         int mHour=c.get(Calendar.HOUR_OF_DAY);
  46.         int mMinute=c.get(Calendar.MINUTE);
  47.         
  48.         /* 跳出TimePickerDialog来设置时间 */
  49.         new TimePickerDialog(EX06_10.this,
  50.           new TimePickerDialog.OnTimeSetListener()
  51.           {                
  52.             public void onTimeSet(TimePicker view,int hourOfDay,
  53.                                   int minute)
  54.             {
  55.               /* 取得设置后的时间,秒跟毫秒设为0 */
  56.               c.setTimeInMillis(System.currentTimeMillis());
  57.               c.set(Calendar.HOUR_OF_DAY,hourOfDay);
  58.               c.set(Calendar.MINUTE,minute);
  59.               c.set(Calendar.SECOND,0);
  60.               c.set(Calendar.MILLISECOND,0);
  61.               
  62.               /* 指定闹钟设置时间到时要运行CallAlarm.class */
  63.               Intent intent = new Intent(EX06_10.this, CallAlarm.class);
  64.               /* 创建PendingIntent */
  65.               PendingIntent sender=PendingIntent.getBroadcast(
  66.                                      EX06_10.this,0, intent, 0);
  67.               /* AlarmManager.RTC_WAKEUP设置服务在系统休眠时同样会运行 
  68.                * 以set()设置的PendingIntent只会运行一次
  69.                * */
  70.               AlarmManager am;
  71.               am = (AlarmManager)getSystemService(ALARM_SERVICE);
  72.               am.set(AlarmManager.RTC_WAKEUP,
  73.                      c.getTimeInMillis(),
  74.                      sender
  75.                     );
  76.               /* 更新显示的设置闹钟时间 */
  77.               String tmpS=format(hourOfDay)+":"+format(minute);
  78.               setTime1.setText(tmpS);
  79.               /* 以Toast提示设置已完成 */
  80.               Toast.makeText(EX06_10.this,"设置闹钟时间为"+tmpS,
  81.                 Toast.LENGTH_SHORT)
  82.                 .show();
  83.             }          
  84.           },mHour,mMinute,true).show();
  85.       }
  86.     });
  87.     /* 只响一次的闹钟的删除Button */
  88.     mButton2=(Button) findViewById(R.id.mButton2);
  89.     mButton2.setOnClickListener(new View.OnClickListener()
  90.     {
  91.       public void onClick(View v)
  92.       {
  93.         Intent intent = new Intent(EX06_10.this, CallAlarm.class);
  94.         PendingIntent sender=PendingIntent.getBroadcast(
  95.                                EX06_10.this,0, intent, 0);
  96.         /* 由AlarmManager中删除 */
  97.         AlarmManager am;
  98.         am =(AlarmManager)getSystemService(ALARM_SERVICE);
  99.         am.cancel(sender);
  100.         /* 以Toast提示已删除设置,并更新显示的闹钟时间 */
  101.         Toast.makeText(EX06_10.this,"闹钟时间解除",
  102.                        Toast.LENGTH_SHORT).show();
  103.         setTime1.setText("目前无设置");
  104.       }
  105.     });
  106.     /* 以下为重复响起的闹钟的设置 */         
  107.     setTime2=(TextView) findViewById(R.id.setTime2);
  108.     /* create重复响起的闹钟的设置画面 */  
  109.     /* 引用timeset.xml为Layout */
  110.     LayoutInflater factory = LayoutInflater.from(this);
  111.     final View setView = factory.inflate(R.layout.timeset,null);
  112.     final TimePicker tPicker=(TimePicker)setView
  113.                                .findViewById(R.id.tPicker);
  114.     tPicker.setIs24HourView(true);
  115.     /* create重复响起闹钟的设置Dialog */   
  116.     final AlertDialog di=new AlertDialog.Builder(EX06_10.this)
  117.           .setIcon(R.drawable.clock)
  118.           .setTitle("设置")
  119.           .setView(setView)
  120.           .setPositiveButton("确定",
  121.             new DialogInterface.OnClickListener()
  122.            {
  123.              public void onClick(DialogInterface dialog, int which)
  124.              {
  125.                /* 取得设置的间隔秒数 */
  126.                EditText ed=(EditText)setView.findViewById(R.id.mEdit);
  127.                int times=Integer.parseInt(ed.getText().toString())
  128.                           *1000;
  129.                /* 取得设置的开始时间,秒及毫秒设为0 */
  130.                c.setTimeInMillis(System.currentTimeMillis());
  131.                c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());
  132.                c.set(Calendar.MINUTE,tPicker.getCurrentMinute());
  133.                c.set(Calendar.SECOND,0);
  134.                c.set(Calendar.MILLISECOND,0);
  135.                /* 指定闹钟设置时间到时要运行CallAlarm.class */
  136.                Intent intent = new Intent(EX06_10.this,
  137.                                           CallAlarm.class);
  138.                PendingIntent sender = PendingIntent.getBroadcast(
  139.                                         EX06_10.this,1, intent, 0);
  140.                /* setRepeating()可让闹钟重复运行 */
  141.                AlarmManager am;
  142.                am = (AlarmManager)getSystemService(ALARM_SERVICE);
  143.                am.setRepeating(AlarmManager.RTC_WAKEUP,
  144.                          c.getTimeInMillis(),times,sender);
  145.                /* 更新显示的设置闹钟时间 */    
  146.                String tmpS=format(tPicker.getCurrentHour())+":"+
  147.                            format(tPicker.getCurrentMinute());
  148.                setTime2.setText("设置闹钟时间为"+tmpS+
  149.                                 "开始,重复间隔为"+times/1000+"秒");
  150.                /* 以Toast提示设置已完成 */
  151.                Toast.makeText(EX06_10.this,"设置闹钟时间为"+tmpS+
  152.                               "开始,重复间隔为"+times/1000+"秒",
  153.                               Toast.LENGTH_SHORT).show();
  154.              }
  155.            })
  156.           .setNegativeButton("取消",
  157.             new DialogInterface.OnClickListener()
  158.            {
  159.              public void onClick(DialogInterface dialog, int which)
  160.              {
  161.              }
  162.            }).create();
  163.     /* 重复响起的闹钟的设置Button */
  164.     mButton3=(Button) findViewById(R.id.mButton3);
  165.     mButton3.setOnClickListener(new View.OnClickListener()
  166.     {
  167.       public void onClick(View v)
  168.       {
  169.         /* 取得点击按钮时的时间作为tPicker的默认值 */
  170.         c.setTimeInMillis(System.currentTimeMillis());
  171.         tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
  172.         tPicker.setCurrentMinute(c.get(Calendar.MINUTE));
  173.         /* 跳出设置画面di */
  174.         di.show();
  175.       }
  176.     });
  177.     /* 重复响起的闹钟的删除Button */
  178.     mButton4=(Button) findViewById(R.id.mButton4);
  179.     mButton4.setOnClickListener(new View.OnClickListener()
  180.     {
  181.       public void onClick(View v)
  182.       {
  183.         Intent intent = new Intent(EX06_10.this, CallAlarm.class);
  184.         PendingIntent sender = PendingIntent.getBroadcast(
  185.                                  EX06_10.this,1, intent, 0);
  186.         /* 由AlarmManager中删除 */
  187.         AlarmManager am;
  188.         am = (AlarmManager)getSystemService(ALARM_SERVICE);
  189.         am.cancel(sender);
  190.         /* 以Toast提示已删除设置,并更新显示的闹钟时间 */
  191.         Toast.makeText(EX06_10.this,"闹钟时间解除",
  192.                        Toast.LENGTH_SHORT).show();
  193.         setTime2.setText("目前无设置");
  194.       }
  195.     });
  196.   }
  197.   /* 日期时间显示两位数的方法 */
  198.   private String format(int x)
  199.   {
  200.     String s=""+x;
  201.     if(s.length()==1) s="0"+s;
  202.     return s;
  203.   }
  204. }