2016-06-11 86 views
1

我使用报警管理器发送了一个报警,但是报警不正常工作。如果时间为18小时,并且我设置了8小时的报警,那么它会在当时发送报警。 这是代码。报警管理器工作不正常

public class Time_Picker extends AppCompatActivity { 
    TimePicker time_picker; 
    String tag="pritesh"; 
    static AlarmManager[] alarmManager=new AlarmManager[5]; 
    static PendingIntent[] pendingIntent = new PendingIntent[5]; 
    EditText message; 
    EditText phone; 
    Intent myIntent; 
    // AlarmManager alarmManager; 
    //PendingIntent pendingIntent; 
    public static String Phone; 
    public static String Message; 
    public int i; 
    array ob=new array(); 
    static ArrayList<PendingIntent> pendingarray=new ArrayList<>(5);  

    public void setAlarm(View view) { 

       //  AlarmManager alarmManager[]=new AlarmManager[5]; 

//   if(prevention.timer==0) { 
       Phone = phone.getText().toString(); 
       Message = message.getText().toString(); 
       phone.setText(""); 
       message.setText(""); 
       //Validating if any field empty 
       if (Phone.length() >= 10 && Message.length() > 0) { 
        Toast.makeText(this.getApplicationContext(), "Your sms will be sent soon", Toast.LENGTH_SHORT).show(); 
        //Getting Calender Reference 
        Calendar cal = Calendar.getInstance(); 
        int currentApiVersion = android.os.Build.VERSION.SDK_INT; 
        if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { 
         cal.set(Calendar.MINUTE, time_picker.getMinute()); 
         cal.set(Calendar.HOUR_OF_DAY, time_picker.getHour()); 
        } else { 
         //Setting the date and time from the time picker 
         cal.set(Calendar.MINUTE, time_picker.getCurrentMinute()); 
         cal.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour()); 
        } 
        int a = (Calendar.getInstance().get(Calendar.SECOND) * 1000); 
        i=array.select; 
        myIntent = new Intent(this, MyReceiver.class); 
        myIntent.putExtra("pos",array.select); 
        //Pending Intent for sending the intent afterwards 
        pendingIntent[array.select] = PendingIntent.getBroadcast(this.getApplicationContext(), array.select, myIntent, 0); 
        alarmManager[array.select] = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE)); 
        alarmManager[array.select].set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent[array.select]); 

        pendingarray.add(pendingIntent[array.select]); 
        sms_list.Phone[array.select]=Phone; 
        Intent back = new Intent(this, sms_list.class); 
        back.putExtra("PHONE", Phone); 
        back.putExtra("Flag",2); 
        back.putExtra("MSG", Message); 
        back.putExtra("HOUR", (int) cal.get(Calendar.HOUR_OF_DAY)); 
        back.putExtra("MIN", (int) cal.get(Calendar.MINUTE)); 

        startActivity(back); 

        //int b; 

       } 
       else 
       { 
        Toast.makeText(this.getApplicationContext(), "Please Fill Up All The Details", Toast.LENGTH_SHORT).show(); 
        array.a[array.select]=false; 
        ob.sort(); 
       } 


    } 

}

即使我设置具有较大的差异它不工作时的代码。

我可以使用12时钟设置闹钟吗?

回答

0

您无法将闹钟设置为已经通过的时间。

alarmManager[array.select].set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent[array.select]); 

请尝试下面的一些未来时间。

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
    SystemClock.elapsedRealtime() + 
    60 * 1000, alarmIntent); 
+0

好的。我明白了实时工作流程IL如何尝试并告诉你它是否正常工作。 – Pritesh

+0

很好。可以请接受n upvote它ans然后 – Sush

+0

对不起,我试过了,但它说我需要15分,但我接受你的答案。 – Pritesh