gpt4 book ai didi

android - 如何设置警报和通知?

转载 作者:行者123 更新时间:2023-11-29 14:05:27 26 4
gpt4 key购买 nike

我一直在做我的警报项目..

我的代码是这样的:

    String a = interval.getText().toString();

Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), Integer.parseInt(a)*1000, pendingIntent);

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.alarm_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(AndroidAlarmService.this, AndroidAlarmService.class);
PendingIntent contentIntent = PendingIntent.getActivity(AndroidAlarmService.this.getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

final int HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID, notification);

Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});

我有一个问题,通知总是出现一次,不会重复出现。

我想设置一个可重复的闹钟和通知,所以每次闹钟激活时,通知也会发出...例如,当我每 1 小时设置一次闹钟,那么每隔一小时,闹钟就会触发器和通知也会出来..

谁能帮帮我?先谢谢

最佳答案

在服务类中传递您的通知,即在 onStartCommand() 中

关于android - 如何设置警报和通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7304487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com