gpt4 book ai didi

安卓通知时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:23 26 4
gpt4 key购买 nike

我现在正在寻找几个小时如何准确地做到这一点:

我希望每天(周末除外)在某个时间(比如 18:00(= 下午 6 点))发送通知,但应用程序已打开时除外。当您收到邮件时,它基本上就像 gmail 应用程序。当用户单击通知时,它应该消失并被带到 MainActivity。

我已经用 AlarmManager 尝试了很多东西,但都没有导致出现通知。

我试过的代码如下:在我的 MainActivity 中:

AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

我的通知服务:

public class NotificationService extends IntentService {



public NotificationService() {
super("NotificationService");
}

@Override
@SuppressWarnings("deprecation")
protected void onHandleIntent(Intent intent) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
nm.notify(1, notification);
}
}

请注意,我使用了已弃用的内容,因为这甚至是在不使用 AlarmManager 时出现通知的唯一方式。如果可能,请使用其中没有已弃用内容但包含最新内容的解决方案:P。

非常非常感谢!!!

致以诚挚的问候

最佳答案

最后我找到了解决方案:

private void handleNotification() {
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
}

这是我的自定义 BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
int dayOfWeek = now.get(Calendar.DATE);
if(dayOfWeek != 1 && dayOfWeek != 7) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.message_box_title))
.setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date));
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
}

这在应用程序标签的 list 中:

<receiver
android:name="be.menis.timesheet.service.AlarmReceiver"
android:process=":remote" />

关于安卓通知时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17053996/

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