2017-11-30 14 views
1

私は、ユーザがサイレントモードでデバイスを設定するための時間を入力し、タイマーが終了すると通常に戻ることのできるアプリケーションを開発中です。これまでは、デバイスをサイレントモードで通常モードに戻すことができますが、サイレントモードがアクティブになるまでに遅延があり、遅延は約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(); 

} 

答えて

0

...... onTimesetの両方で機能する ()Iが最終のキーワードを削除し、現在何とか遅延がなくなり、デバイスはその場で静かになります。