预定的警报管理器无法在Android上运行

编程入门 行业动态 更新时间:2024-10-28 00:22:44
本文介绍了预定的警报管理器无法在Android上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试启动每天在特定时间重复的警报服务。关于这一点,我在堆栈溢出中经历了很多线程,但是没有运气。我遵循了一些教程: http:// karanbalkar/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ 和 javatechig/android/repeat-alarm-example-in-android

I am trying to start an alarm service that repeats every day at a particular time. I have gone through a lot of threads on stack overflow regarding this but no luck. I followed a few tutorials: karanbalkar/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ and javatechig/android/repeat-alarm-example-in-android

我的服务从来没有开始,我不明白为什么。以下是我的代码:

My service is never started and I do not understand why. Below is my code:

我的清单文件:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <application> <service android:name="com.paper.DownloadService" android:enabled="true"/> <receiver android:name="com.paper.MyReceiver" ></receiver> </application>

我的接收器类别:

public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context rcontext, Intent intent) { Log.e("Main Activity", "inside on receive of myreceiver"); Intent service1 = new Intent(rcontext, DownloadService.class); rcontext.startService(service1); } }

我的服务等级:

public class DownloadService extends Service { @Override public void onCreate() { // TODO Auto-generated method stub Log.e("Download Service", "CREATED"); } @SuppressLint({ "SimpleDateFormat", "NewApi" }) @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.e("Download Service", "STARTED"); return START_NOT_STICKY; } }

我的主要活动(创建方法内部):

My Main Activity (Inside On Create Method):

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 15); calendar.set(Calendar.MINUTE, 29); Intent myIntent = new Intent(this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

在这里,我试图在每天下午3:29设置闹钟,但该服务确实在那个时候或任何时候都没有开始。任何帮助,将不胜感激。谢谢!

Here, I am trying to set up my alarm at 3:29 pm every day but the service does not get started at that time or any time for that matter. Any help would be appreciated. Thanks!

推荐答案

这就是我要使其正常工作的方法:

Here is what I did to get it working:

1)在我的清单文件中添加了< uses-permission android:name = com.android.alarm.permission.SET_ALARM /> 。

1) Added <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> to my manifest file.

2)将我的主要活动中的代码更改为:

2) Changed code in my Main activity to:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR, 3); calendar.set(Calendar.MINUTE, 29); calendar.set(Calendar.AM_PM, Calendar.PM); Intent myIntent = new Intent(this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

希望有人发现这有帮助!

Hope someone finds this helpful!

更多推荐

预定的警报管理器无法在Android上运行

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

发布评论

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

>www.elefans.com

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