Android中AlarmManager使用示例(2)

1 import android.app.AlarmManager; 2 import android.app.PendingIntent; 3 import android.app.TimePickerDialog; 4 import android.content.Intent; 5 import android.provider.Settings; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.TextView; 10 import android.widget.TimePicker; 11 import java.util.Calendar; 12 public class MainActivity extends AppCompatActivity { 13 AlarmManager alarmManagerSet; 14 PendingIntent pendingIntentSet; 15 int myHour,myMinute;//声明响铃时间 16 int intervalHour,intervalMinute,intervalSecond;//声明间隔时间(响铃周期) 17 Calendar calendar;//声明日历 18 TextView time_tv,time_tv2; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 alarmManagerSet= (AlarmManager) getSystemService(ALARM_SERVICE); 25 //设置声明时间格式 26 calendar = Calendar.getInstance(); 27 myHour=calendar.get(Calendar.HOUR_OF_DAY); 28 myMinute=calendar.get(Calendar.MINUTE); 29 intervalHour=calendar.get(Calendar.HOUR_OF_DAY); 30 intervalMinute=calendar.get(Calendar.MINUTE); 31 //获取时间设置的TextView 32 time_tv= (TextView) findViewById(R.id.time_tv); 33 time_tv2= (TextView) findViewById(R.id.time_tv2); 34 } 35 //使用timePickerDialog设置闹铃响铃时间 36 public void setTime(View view){ 37 TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 38 @Override 39 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 40 myHour=hourOfDay; 41 myMinute=minute; 42 time_tv.setText(myHour+":"+myMinute); 43 } 44 },myHour,myMinute,true); 45 timePickerDialog.show(); 46 } 47 //使用timePickerDialog设置闹铃响铃周期(间隔) 48 public void setIntervalTime(View view){ 49 TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 50 @Override 51 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 52 intervalHour=hourOfDay; 53 intervalMinute=minute; 54 time_tv2.setText(intervalHour+":"+intervalMinute); 55 } 56 },intervalHour,intervalMinute,true); 57 timePickerDialog.show(); 58 } 59 public void open(View view){ 60 //分别获取闹铃响铃时间以及闹铃间隔时间(用冒号分割存储于数组中) 61 String[] triggerTime=time_tv.getText().toString().split(":"); 62 String[] intervalTime=time_tv2.getText().toString().split(":"); 63 //通过Calendar分别获取对应的小时和分钟(需要将String转换为int) 64 Calendar calendar1 = Calendar.getInstance(); 65 calendar1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(triggerTime[0])); 66 calendar1.set(Calendar.MINUTE,Integer.parseInt(triggerTime[1])); 67 //得到点击触发的毫秒值(即闹钟提醒时间) 68 long triggerAtMillis= calendar1.getTimeInMillis(); 69 //设定pendinngIntent接受自定义闹铃广播 70 pendingIntentSet = PendingIntent.getBroadcast(this,0,new Intent("repeatAlarm"),0); 71 //判断如果当前系统时间大于设置的闹铃时间,则在第二天开始启用该闹铃 72 if(System.currentTimeMillis()>triggerAtMillis){ 73 triggerAtMillis=triggerAtMillis+24*60*60*1000; 74 } 75 //使用.setRepeating设置重复闹铃 76 alarmManagerSet.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,intervalMillis,pendingIntentSet); 77 } 78 public void stop(View v){ 79 alarmManagerSet.cancel(pendingIntentSet); 80 } 81 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2ba4e18d49c5c06cbeb5b3ca820829d1.html