Android Alarm

1>  用户添加闹钟后,是什么时候开始检查的? 怎么检查的?

When:Trigger the check in 5s from now.

//接受者

CalendarProvider2

 case REMINDERS:

{

    id = mDbHelper.remindersInsert(values);

 // Trigger the check in 5s from now
            long triggerAtTime = SystemClock.elapsedRealtime() + ALARM_CHECK_DELAY_MILLIS;
            set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pending);

      Intent intent = new Intent(ACTION_CHECK_NEXT_ALARM);

// ..................

      mAlarmManager.set(type, triggerAtTime, operation); //设置闹钟,给定触发时间。

}


//接受者
CalendarProviderBroadcastReceiver {

onReceive {

//com.android.providers.calendar.intent.CalendarProvider2

  context.startService(CalendarProviderIntentService);

}

}

//处理者

CalendarProviderIntentService {

onHandleIntent ->CalendarAlarmManager.runScheduleNextAlarm

->CalendarAlarmManager.scheduleNextAlarmLocked

-> CalendarContract.CalendarAlerts.scheduleAlarm(mContext, mAlarmManager, alarmTime);

->  scheduleNextAlarmCheck(nextAlarmTime + DateUtils.MINUTE_IN_MILLIS);



}

请求者。

CalendarAlarmManager

{

->   Intent intent = new Intent(CalendarReceiver.SCHEDULE);

}

//接受者

CalendarReceiver {

onReceive {

//com.android.providers.calendar.SCHEDULE_ALARM

//处理者

ExecutorService.submit-> cr.update(CalendarAlarmManager.SCHEDULE_ALARM_URI, null /* values */, null /* where */, null /* selectionArgs */);

}

}

2>闹钟是什么时候开始唤醒的?怎么唤醒的?

a->action android:name="android.intent.action.BOOT_COMPLETED" 

  ->AlertReceiver

 ->context.startService(AlertService);

b)->AlertService ->processMessage->InitAlarmsService->onHandleIntent->


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AndroidAlarmManager可以用于在指定时间触发应用程序中的操作,其中包括本地通知。下面是一个简单的示例代码,可以在指定时间触发通知。 首先,在AndroidManifest.xml文件中添加以下权限: ``` <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> ``` 然后,在应用程序代码中,创建一个PendingIntent对象,它将被发送到AlarmManager以触发通知。在PendingIntent对象中,设置通知的标题,内容和其他属性。以下是示例代码: ```java Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification Title") .setContentText("My Notification Content") .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 接下来,使用AlarmManager设置一个定时器,以便在指定的时间触发通知: ```java AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, MyAlarmReceiver.class); intent.putExtra("notificationId", notificationId); intent.putExtra("message", message); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTimeInMillis, pendingIntent); ``` 最后,创建一个BroadcastReceiver类来接收AlarmManager发送的广播,并在接收到广播时触发通知: ```java public class MyAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int notificationId = intent.getIntExtra("notificationId", 0); String message = intent.getStringExtra("message"); Intent intent1 = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification Title") .setContentText(message) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build()); } } ``` 这样,当AlarmManager触发广播时,MyAlarmReceiver类将接收到广播并触发通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值