gpt4 book ai didi

android - Android 中自定义通知的确切时间

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:28 24 4
gpt4 key购买 nike

我正在开发一个用于咨询服务的 Android 应用程序。客户可以在应用程序中查看他们的预定约会。例如,

下一次约会:2016 年 12 月 31 日上午 10:00

现在我需要做的是让用户收到 2 条通知——关于约会的提醒。一个在 7 天前,另一个在 3 天前。我将这个日期(2016 年 12 月 31 日上午 10:00)保存为 String,这样我就可以提取年、月等。我发现我需要编写某种服务来发送这些通知。这是我试过的(未完成):

public class NotificationService extends Service {
@Override
public void onCreate() {
Intent resultIntent=new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Notification nBuilder = new Notification.Builder(this)
.setContentTitle("Don't miss! ")
.setTicker("Notification!")
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.my_logo)
.setContentText("7 days left till your appointment...")
//.setWhen(System.currentTimeMillis())
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nBuilder.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1,nBuilder);
}

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

还有我不知道从哪里调用的方法:

public void reminder() {
Intent intent = new Intent(getActivity(), MainActivity.class);

AlarmManager manager =(AlarmManager) getActivity().getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getActivity().getApplicationContext(),
0,intent, 0);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),24*60*60*1000,pendingIntent);
}

出于测试目的,我手动设置了小时/分钟/秒,但显然我需要从日期 String 中提取它。

最佳答案

你需要写一个IntentService第一的。这是一个示例,您可以编写代码来显示 processNotification 中的通知功能。

public class NotificationIntentService extends IntentService {

private static final String ACTION_START = "ACTION_START";

public NotificationIntentService() {
super(NotificationIntentService.class.getSimpleName());
}

public static Intent createIntentStartNotificationService(Context context) {
Intent intent = new Intent(context, NotificationIntentService.class);
intent.setAction(ACTION_START);
return intent;
}

@Override
protected void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (ACTION_START.equals(action))
processNotification();

} finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}

private void processNotification() {
Intent resultIntent=new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Notification nBuilder = new Notification.Builder(this)
.setContentTitle("Don't miss! ")
.setTicker("Notification!")
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.my_logo)
.setContentText("7 days left till your appointment...")
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nBuilder.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, nBuilder);
}
}

然后创建一个NotificationEventReceiver

public class NotificationEventReceiver extends WakefulBroadcastReceiver {

private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";

public static void setupAlarm(Context context, long interval) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);

alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, alarmIntent);
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Intent serviceIntent = null;
if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) {
serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);
}

if (serviceIntent != null) {
startWakefulService(context, serviceIntent);
}
}

private static PendingIntent getStartPendingIntent(Context context) {
Intent intent = new Intent(context, NotificationEventReceiver.class);
intent.setAction(ACTION_START_NOTIFICATION_SERVICE);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

还有 NotificationServiceStarterReceiver

public final class NotificationServiceStarterReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
long interval = getIntent().getLongExtra("alarm_interval", 0);
NotificationEventReceiver.setupAlarm(context, interval);
}
}

将这些添加到您的 AndroidManifest.xml 中里面<application>标签

<service
android:name="YourPackage.NotificationIntentService"
android:enabled="true"
android:exported="false" />

<receiver android:name="YourPackage.BroadcastReceiver.NotificationEventReceiver" />
<receiver android:name="YourPackage.BroadcastReceiver.NotificationServiceStarterReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>

现在从您的 Activity你可以调用setupAlarm()里面onCreate功能。

NotificationEventReceiver.setupAlarm(getApplicationContext(), interval);

您需要添加 WAKE_LOCK list 中的许可。

<uses-permission android:name="android.permission.WAKE_LOCK" />

在这里你可以看到你可以传递 interval要显示的下一个通知。使用 interval明智地。您可以考虑将约会的当前状态保存在数据库中,然后在必要时通过传递下一个警报的适当间隔来触发警报。就是这个主意。

更新

因此在您的情况下,您不想在用户注销时显示通知。所以在这种情况下,您可以考虑保留 SharedPreference存储登录状态。你可以调用 processNotification基于存储值的函数。

所以伪代码可能是这样的。

if(pref.getBoolean("login_status", false)) {
// If the login status is true, process the notification
processNotification();
} else {
// Do nothing
}

关于android - Android 中自定义通知的确切时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407084/

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