First, you need an entry before setting the reminder, for example, in The onclick event.
In android, it is actually very simple to use the alarm clock. You only need to tell the system when you want to be reminded, and then need a broadcast receiver of the alarm clock. When you set the time, the system will send you a broadcast. After receiving the broadcast, you can perform some operations, such as starting your app or redirecting to any interface in your app. Code is directly written without much nonsense.
01 |
// Send an alarm request |
02 |
Intent intent = new Intent(mContext, AlarmReceiver. class ); |
03 |
intent.setAction( "something" ); |
04 |
intent.setType( "something" ); |
05 |
intent.setData(Uri.EMPTY); |
06 |
intent.addCategory(“something”); |
07 |
intent.setClass(context, AlarmReceiver. class ); |
08 |
// The four attributes set for intent are used to distinguish the alarm request you send to the system. When you want to cancel the alarm request, these four attributes must be strictly equal, therefore, you need some unique attributes, such as some specific fields in the json returned by the server. |
09 |
// Of course, intent can also include some messages you want to transmit. |
10 |
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmCount, intent, 0 ); |
11 |
// AlarmCount is the number of alarms you need to record. Make sure that the number of alarms you send cannot be the same. Set the last parameter to 0. |
12 |
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
13 |
am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); |
14 |
// The request for the alarm is sent out. Time is the time you want to be reminded, in milliseconds. Note that it is not the time difference. The first parameter reminds me of the requirements I have given. Interested friends can go to google. There are a lot of materials in this regard. There are a variety of materials. Let's take a look at the differences. |
15 |
// Cancel the alarm request |
16 |
Intent intent = new Intent(mContext, AlarmReceiver. class ); |
17 |
intent.setAction( "something" ); |
18 |
intent.setType(something); |
19 |
intent.setData(Uri.EMPTY); |
20 |
intent.addCategory(something); |
21 |
intent.setClass(context, AlarmReceiver. class ); |
22 |
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alarmCount, intent, 0 ); |
23 |
// AlarmCount corresponds to the alarmCount you set, |
24 |
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
25 |
am.cancel(pendingIntent); |
26 |
// Next, you need a broadcast receiving class: |
27 |
public class AlarmReceiver extends BroadcastReceiver{ |
29 |
private NotificationManager manager; |
32 |
public void onReceive(Context context, Intent intent) { |
33 |
manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); |
34 |
// For example, this id is uploaded by you. |
35 |
String id = intent.getStringExtra( "id" ); |
36 |
// MainActivity is the Activity you want to jump to when you click the notification |
37 |
Intent playIntent = new Intent(context, MainActivity. class ); |
38 |
playIntent.putExtra( "id" , id); |
39 |
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1 , playIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
40 |
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); |
41 |
builder.setContentTitle( "title" ).setContentText( "Reminder content" ).setSmallIcon(R.drawable.app_icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel( true ).setSubText( "Level 2 text" ); |
42 |
manager.notify( 1 , builder.build()); |
Here, the alarm function is basically complete. If you have any questions, leave a message.