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());
}
}
죄송합니다 아직 아무것도. onClick()을 사용하면 시간 제한을 제거하고 알림을 표시하지만 위에 나온 코드 중 아무 것도 사용하지 않습니다. ( –
AndroidManifest.xml에 BroadcastReceiver가 선언되어 있습니까? onClick()에 Intent를 보내어 문제는 AlarmManager 또는 Receiver입니다 –
OnClick에서 알림은 작동합니다 –