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

编程入门 行业动态 更新时间:2024-10-26 14:33:25
本文介绍了Android中自定义通知的确切时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am developing an Android application for counseling services. Client can view their scheduled appointment in app. For example,

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

Next appointment: Dec 31 2016 10:00AM

现在我需要做的是,该用户将收到2条通知-有关约会的提醒。一个在7天之前,另一个在3天之前。我将此日期(2016年12月31日上午10:00)保存为 String ,以便提取年份,月份等。我发现我需要写一些一种将发送这些通知的服务。这是我尝试过的(未完成):

Now I need to do that user will receive 2 notifications – reminders about appointment. One on 7 days before and another on 3 days before. I save this date (Dec 31 2016 10:00AM) as a String so I can extract year, month, etc. I found that I need to write some kind of service which will send these notifications. This is what I tried (is not completed):

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; } }

我不知道从哪里知道的方法呼叫:

And method that I don't know from where to call:

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 中提取它。

For testing purposes I have set hour/minute/second manually but obviously I will need to extract it from date String.

推荐答案

您需要先编写 IntentService 。这是一个示例,您可以编写代码以在 processNotification 函数中显示通知。

You need to write an IntentService first. Here's an example, you can write code for showing the notification in processNotification function.

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 { WakefulBroadcastReceiverpleteWakefulIntent(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> 标记内

Add these in your AndroidManifest.xml inside <application> tag

<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>

现在从您的活动开始, onCreate 函数内的 setupAlarm()。

Now from your Activity you may call the setupAlarm() inside onCreate function.

NotificationEventReceiver.setupAlarm(getApplicationContext(), interval);

您需要在您的帐户中添加 WAKE_LOCK 权限表现。

You need to add WAKE_LOCK permission in your manifest.

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

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

Here you see you can pass the interval of the next notification to be shown. Use the interval wisely. You might consider saving the current statuses of the appointment in the database and then trigger the alarm when necessary by passing proper interval of next alarm. That's the idea.

更新

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

So in your case, you don't want to show the notification when user is logged out. So in this case, you might consider keeping a SharedPreference to store the login status. You might call the processNotification function based on the value stored.

所以伪代码可能看起来像这样。

So the pseudo code may look like this.

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

更多推荐

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

本文发布于:2023-11-27 05:39:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   确切   通知   时间   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!