延迟静音设备

时间:2017-11-30 08:00:33

标签: java timepicker

我正在开发一个应用程序,用户可以在该应用程序中输入设置为静音模式的时间,并在计时器结束时恢复正常。到目前为止,我可以将设备置于静音模式并返回正常模式,但是有一段延迟,之后静音模式被激活,延迟大约为10或15秒。我不明白为什么静音模式被延迟激活。下面是我的start()和end()时间函数的代码。

开始():

 public void start()
  {

      Calendar cal=Calendar.getInstance();
   // simpleDateFormat=new SimpleDateFormat("hh:mm a");
    Date date = new Date();
    //String time=simpleDateFormat.format(date);
    int hour=cal.get(Calendar.HOUR);
    int minute=cal.get(Calendar.MINUTE);

    timePickerDialog=new TimePickerDialog(MainActivity.this, new
            TimePickerDialog.OnTimeSetListener()
            {

                @Override
                public void onTimeSet(TimePicker view,  final int hourOfDay1,final int minute1) {
                    Time time = new Time(hourOfDay1, minute1,0);
                    GregorianCalendar j2=new GregorianCalendar(hourOfDay1,minute1,0);
                    System.out.println(j2);
                    //little h uses 12 hour format and big H uses 24 hour format
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");

                    //format takes in a Date, and Time is a sublcass of Date
                    String s = simpleDateFormat.format(time);
                    start.setText(s);


                      //dp.getDayOfMonth();

                    Calendar calNow = Calendar.getInstance();
                    Calendar calSet = (Calendar) calNow.clone();

                    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay1);
                    calSet.set(Calendar.MINUTE, minute1);
                    Toast.makeText(MainActivity.this,"Starting Pending intent started",Toast.LENGTH_LONG).show();
                    //final int _id = (int) System.currentTimeMillis();
                    Intent intent = new Intent(getBaseContext(), SilenceBroadCastReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
                    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, calNow.getTimeInMillis(), pendingIntent);

                    //final long sttimer=((shour)*60*60*1000)+((sminute)*60*1000);


                }
            },hour,minute,false);
            timePickerDialog.setTitle("Set Start time");
            timePickerDialog.show();



}

端()

    public void end(){
    Calendar cal1=Calendar.getInstance();
    //simpleDateFormat1=new SimpleDateFormat("hh:mm a");
    Date date1 = new Date();
    //String time=simpleDateFormat1.format(date1);
    int hour=cal1.get(Calendar.HOUR);
    int minute=cal1.get(Calendar.MINUTE);

    secondtimepickerdialog=new TimePickerDialog(MainActivity.this, new
            TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view,  final int hourOfDay1,final int minute1) {
                    Time time = new Time(hourOfDay1, minute1,0);
                    GregorianCalendar j3=new GregorianCalendar(hourOfDay1,minute1,0);
                    //little h uses 12 hour format and big H uses 24 hour format
                    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("h:mm a");

                    //format takes in a Date, and Time is a sublcass of Date
                    String s1 = simpleDateFormat1.format(time);
                    end.setText(s1);

                    Calendar calNow1 = Calendar.getInstance();
                    Calendar calSet1 = (Calendar) calNow1.clone();

                    calSet1.set(Calendar.HOUR_OF_DAY, hourOfDay1);
                    calSet1.set(Calendar.MINUTE, minute1);
                    Toast.makeText(MainActivity.this,"End Pending intent started",Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(getBaseContext(), UnsilenceBroadcastReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_2, intent, PendingIntent.FLAG_ONE_SHOT);
                    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, j3.getTimeInMillis(), pendingIntent);

                    //final long sttimer=((shour)*60*60*1000)+((sminute)*60*1000);


                }
            },hour,minute,false);
    secondtimepickerdialog.setTitle("Set End time");
    secondtimepickerdialog.show();

}

1 个答案:

答案 0 :(得分:0)

解决...... 在onTimeset()的两个函数中,我删除了最后一个关键字,现在某种程度上延迟消失了,设备在现场变得安静。