闹钟在安卓上不起作用?

发布于 2024-12-16 20:20:37 字数 547 浏览 5 评论 0原文

我被一些我认为非常琐碎的事情所困扰。 基本上,我正在为未来的某个特定时刻安排闹钟:

Intent contentIntent = new Intent(this, AlarmReceiver.class); 
PendingIntent theappIntent = PendingIntent.getService(Main.this, 0,contentIntent, 0); 
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 

在移动设备持续开启的情况下工作正常。 我的问题是手机重启后没有触发警报 并在预期时间打开。 有什么我错过的吗?我应该做一些额外的工作来保持警惕吗 重启后安排?

I get stuck with something that , I guess , is very trivial.
Basically I am scheduling alarm for a given moment in the future :

Intent contentIntent = new Intent(this, AlarmReceiver.class); 
PendingIntent theappIntent = PendingIntent.getService(Main.this, 0,contentIntent, 0); 
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 

works fine while mobile is constantly on.
My problem is that alarm is not triggered after mobile is restarted
and is TURNED ON on the expected time.
Is there something I missed? Should I do some extra work to keep alarm
scheduled after restart?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-12-23 20:20:37

当设备启动时,您必须重置警报。
当设备启动并重置警报时,您缺少检测应用程序。所以下面是在清单中编写的代码

<receiver android:name=".YourReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

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

同时创建一个 BroadcastReceiver 来接收启动完成的意图

 public class YourReceiver extends BroadcastReceiver {


 Context ct;

 @Override
 public void onReceive(Context context, Intent intent) 
 {
    ct=context;
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    {
      // Reset your alarm here
      Intent contentIntent = new Intent(ct, AlarmReceiver.class); 
      PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0); 
      Calendar calendar = Calendar.getInstance();
      calendar.set(year, month, day, hour,minute); 
      AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 
    }
 }

You have to reset the alarm when device get boot.
You are missing to detect the app when device boot and reset the alarm..so below is code written in manifest

<receiver android:name=".YourReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

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

Also create a BroadcastReceiver to receive the boot completed intent

 public class YourReceiver extends BroadcastReceiver {


 Context ct;

 @Override
 public void onReceive(Context context, Intent intent) 
 {
    ct=context;
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    {
      // Reset your alarm here
      Intent contentIntent = new Intent(ct, AlarmReceiver.class); 
      PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0); 
      Calendar calendar = Calendar.getInstance();
      calendar.set(year, month, day, hour,minute); 
      AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 
    }
 }
倾城°AllureLove 2024-12-23 20:20:37

您应该添加一个接收器来监听 android.intent.action.BOOT_COMPLETED 并从那里启动闹钟。

You should add an receiver For listening android.intent.action.BOOT_COMPLETED and start your alarm from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文