gpt4 book ai didi

java - 每天特定时间通知不通知

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:21 25 4
gpt4 key购买 nike

我想每天上午 8:00 生成通知。我已经创建了简单通知的代码,但没有收到通知,以下是 NotifyService 类的代码

    public class NotifyService extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*Notification Related*/
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Thought")
.setContentText("Get Today's Thought");
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
//mBuilder.setAutoCancel(true);

// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}

这是MainActivity类onCreate方法中编写的代码

PendingIntent pendingIntent;
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,8);
calendar1.set(Calendar.MINUTE,00);
calendar1.set(Calendar.SECOND,00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);

不知道哪里错了

最佳答案

我认为您没有在这里创建服务。 Notifyservice 类扩展了 AppCompatActivity。您应该扩展 Service 类以使其成为一项服务。

关于java - 每天特定时间通知不通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981384/

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