Scheduled Alarm Manager not working with Android

I am trying to start the alarm service, which is repeated every day at a specific time. I spent a lot of threads overflowing the stack on this, but no luck. I followed a few tutorials: http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ and http://javatechig.com/android/repeat-alarm-example -in-android

My service never starts, and I don’t understand why. Below is my code:

My manifest file:

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

My receiver class:

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

My class of service:

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

Here I try to set the alarm at 15:29 every day, but the service does not start at this time or at any time. Any help would be greatly appreciated. Thanks!

+5
source share
1 answer

Here is what I did to make it work:

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

2) Changed the code in my main activity:

  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!

+10
source

Source: https://habr.com/ru/post/1214964/


All Articles