2017-09-21 3 views
1

Im 그냥 새로운 안드로이드와 인스턴트 메신저에서 현재 일하고 있습니다. 이것은 SQLite db 값을 사용하여 알람을 설정하는 코드입니다. 여기 내 문제는 알람이 임의의 시간에 트리거된다는 것입니다.안드로이드 알람 관리자 - 임의의 시간에 알람 발화

공공 무효 startAlarm는()는 ParseException를 슬로우 {

Date dt = new Date(); 
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a"); 
    String time = sdf.format(dt); 
    String start_time; 

    Calendar sCalendar = Calendar.getInstance(); 
    String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()); 

    DatabaseHelper helper = new DatabaseHelper(getApplicationContext()); 
    SQLiteDatabase sqLiteDatabase = helper.getReadableDatabase(); 
    String checktime = "Select * from SCHEDULE where week_day='"+dayLongName+"'"; 

    Cursor cursor = sqLiteDatabase.rawQuery(checktime, null); 
     if(cursor.moveToNext()) { 

      start_time = cursor.getString(cursor.getColumnIndex(DatabaseHelper.STARTTIME)); 

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a",Locale.getDefault()); 
      SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("hh:mm",Locale.getDefault()); 

      Date milli =simpleDateFormat2.parse(start_time); 
      String milli2 = simpleDateFormat2.format(milli); 

      Date timeparsed1 = simpleDateFormat.parse(start_time); 
      String timeparsed2 = simpleDateFormat.format(timeparsed1); 

       String s = milli2; 
       Pattern p = Pattern.compile("(\\d+):(\\d+)"); 
       Matcher m = p.matcher(s); 

       if (m.matches()) { 
        int hrs = Integer.parseInt(m.group(1)); 
        int min = Integer.parseInt(m.group(2)); 

        long ms = (long) hrs * 60 * 60 * 1000 + min * 60 * 1000; 
      //  System.out.println("hrs=" + hrs + " min=" + min + " ms=" + ms); 

        Intent intent = new Intent(this, MyBroadcastReceiver.class); 

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); 

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

        alarmManager.set(AlarmManager.RTC_WAKEUP, ms , pendingIntent); 

       //Toast.makeText(getApplicationContext(),String.valueOf(ms),Toast.LENGTH_SHORT).show(); 

       } else { 

        Toast.makeText(getApplicationContext(),"Bad Format",Toast.LENGTH_SHORT).show(); 

       } 
} 
+0

당신은 [이 참조] 수 (https://stackoverflow.com/a/35127736/5860777) 나는 –

답변

0

안드로이드 4.4 이후, 구글은 전력 사용을 최소화하기 위해 OS 전력 관리 메커니즘을 도입했다. 아래 코드와 비슷한 코드를 추가하십시오. 버전이 KitKat 이상이면 setExact()를 사용하십시오. 그렇지 않으면 set()을 사용하십시오.

 //For Android version 4.4 up, use setExact() for the alarm 
     //otherwise use set() 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent); 
     } else { 
      alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent); 
     } 

Documentation reference here

+0

좋아요 감사합니다. –

+0

가 동일 응용 프로그램을 실행 한 후, 알람 3 초 후에 트리거하려고합니다 –

+0

3 초 후에도 일관성이 있습니까? – ADimaano

0

이 서비스는 결코 나를 위해 작동 중지 않을 코드의 집합을 시도해보십시오

아래의 예를 참조하십시오. "setInexactReapeating"은 일단 롤리팝과 마시맬로 장치가 작동하면 많은 도움이되지 않습니다. 그러면 서비스를 제어 할 수 없기 때문에 도움이 될 것입니다.

if (android.os.Build.VERSION.SDK_INT >= 23) 
{ 
    piLR = PendingIntent.getBroadcast(context, 0, intentLR, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       interval, piLR); 
} 
else if (android.os.Build.VERSION.SDK_INT >= 19 
      && android.os.Build.VERSION.SDK_INT < 23) 
{ 
    piLR = PendingIntent.getBroadcast(context, 0, intentLR, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    amLR.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), interval, piLR); 
} 
else 
{ 
    amLR.setRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), interval, piLR); 
}