0

오전 9시에 자동 알림을 만들려고합니다.시간 초과 알림 (Android)과 관련된 몇 가지 문제가 있습니까?

다음 코드를 만들었지 만 자동 알림을받을 수 없습니다.

그리고 Android Manifest 파일에 필요한 권한을 추가했습니다. setAlarm();

public void setAlarm() { 

    AlarmManager alarmMgr; 
    PendingIntent alarmIntent; 

    alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(getApplicationContext(), Notifications.class); 
    alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Calendar calendar_now = Calendar.getInstance(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 9); 
    calendar.set(Calendar.MINUTE, 30); 

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, alarmIntent); 

    if(calendar_now.getTimeInMillis() == calendar.getTimeInMillis()) { 
    new Notifications().createNotification(getApplicationContext(), getIntent(), "Stock-Market Alert", "The Market opens shortly."); 
    } 

} 

Notifications.class은 다음과 같다 : 다음 MainActivity.class 'onCreate 방법에서

은 내가 함수를 호출

public class Notifications extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
    String notificationTitle = "Default Title", notificationMessage = "Default Message"; 

    createNotification(context, intent, notificationTitle, notificationMessage); 
    } 

    public void createNotification(Context context, Intent intent, String notificationTitle, String notificationMessage) { 

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, 
       new Intent(context, Repeating_activity.class), PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setSmallIcon(android.R.drawable.arrow_up_float) 
      .setContentTitle(notificationTitle) 
      .setContentText(notificationMessage) 
      .setTicker("Alert Message !") 
      .setAutoCancel(true) 
      .setSmallIcon(android.R.drawable.stat_sys_warning); 

    builder.setContentIntent(pendingIntent); 
    builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE); 
    builder.setDefaults(NotificationCompat.DEFAULT_SOUND); 

    notificationManager.notify(1001, builder.build()); 
    } 
} 

답변

0

당신이 밀리 초를 비교하기 위해 노력하고 있기 때문에 이런 일이 , 드물게 당신은 같은 밀리 세컨드를 맞추지 않을 것입니다 (거의 말하지 않기 위해). 또한 논리를 추가하여 수신자의 시간을 비교하십시오. 다음 코드를 시도하지 :

public void onReceive(Context context, Intent intent) { 
    Calendar cal = Calendar.getInstance(); 
    int currentHour = cal.get(Calendar.HOUR); 
    int currentMinute = cal.get(Calendar.MINUTE); 
    if (currentHour == 9 && currentMinute == 00) { 
     String notificationTitle = "Default Title", notificationMessage = "Default Message"; 
     createNotification(context, intent, notificationTitle, notificationMessage); 
    } 
} 

그리고 당신의에서 setalarm() 메서드는이 같은 간단한 될 수 있습니다

public void setAlarm(){ 
     AlarmManager alarmMgr; 
     PendingIntent alarmIntent; 

     alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(getApplicationContext(), Notifications.class); 
     alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, alarmIntent); 

    } 
+0

죄송합니다 아직 아무것도. onClick()을 사용하면 시간 제한을 제거하고 알림을 표시하지만 위에 나온 코드 중 아무 것도 사용하지 않습니다. ( –

+0

AndroidManifest.xml에 BroadcastReceiver가 선언되어 있습니까? onClick()에 Intent를 보내어 문제는 AlarmManager 또는 Receiver입니다 –

+0

OnClick에서 알림은 작동합니다 –

관련 문제