gpt4 book ai didi

android - 即使应用程序已关闭,如何每天在特定时间显示通知?

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

虽然之前在 Stack Overflow 上可能有人问过这个问题,但我仍然没有找到明确的答案。

例如,我想在每天中午 12 点显示通知,即使应用已关闭。我从这些链接中引用了:Notifications in specific time every day android , Android daily repeating notification at specific time of a day using AlarmManager , Android BroadcastReceiver on startup - keep running when Activity is in Background还有更多……我对 Service 和 BroadcastReceiver 之间的区别感到困惑。我应该使用哪一个?还是我应该同时使用它们?

到目前为止,我知道如何显示通知,但我不知道如何每天自动显示一次,即使应用已关闭也是如此。

我的代码:

public class NotifyService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();

Intent resultIntent = new Intent(this, HomeScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

Notification.Builder notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("App Title")
.setContentText("Some Text...")
.setContentIntent(resultPendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT < 16) {
notificationManager.notify(1, notification.getNotification());
} else {
notificationManager.notify(1, notification.build());
}
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
}
}

AppManifest.xml:

<service android:name=".NotifyService" />

我应该如何编写代码来完成我想要的?我可以从中理解的任何建议或任何好的链接?

最佳答案

这是更新的解决方案,适用于 Android Oreo

第 1 步:在您的 MainActivity 中创建一个方法,并使用 AlarmManager 在指定时间设置闹钟。

public void myAlarm() {

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 47);
calendar.set(Calendar.SECOND, 0);

if (calendar.getTime().compareTo(new Date()) < 0)
calendar.add(Calendar.DAY_OF_MONTH, 1);

Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

if (alarmManager != null) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

}

}

我将闹钟设置在 09:47 PM

第 2 步: 创建 BroadcastReceiver 以在警报发生时监听

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

NotificationHelper notificationHelper = new NotificationHelper(context);
notificationHelper.createNotification();

}
}

我正在创建名为 NotificationReceiver 的类并扩展 BroadcastReceiver,在 onReceive 中有一个名为 NotificationHelper 的类,不要混淆我会在接下来的步骤中解释这个类。

第 3 步:创建通知类

class NotificationHelper {

private Context mContext;
private static final String NOTIFICATION_CHANNEL_ID = "10001";

NotificationHelper(Context context) {
mContext = context;
}

void createNotification()
{

Intent intent = new Intent(mContext , NotificationActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Title")
.setContentText("Content")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);

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

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}

这个类处理通知

第 4 步:回到第 2 步:并调用通知类

 NotificationHelper notificationHelper = new NotificationHelper(context);
notificationHelper.createNotification();

注册广播接收器转到您的 Androidmanifest 文件并注册您的广播接收器

<receiver
android:name=".NotificationReceiver"></receiver>

有关更多信息,请参阅 this来自谷歌的指南

希望对你有帮助。

关于android - 即使应用程序已关闭,如何每天在特定时间显示通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33055129/

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