5

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: 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 is never started and I do not 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 Service Class:

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

Shreya
  • 241
  • 1
  • 2
  • 11
  • Is this `Log.e("Main Activity", "inside on receive of myreceiver");` display in `Log`?? Also what is the value of `calendar.getTimeInMillis(),` and `AlarmManager.INTERVAL_DAY`? – Piyush Mar 09 '15 at 10:21
  • did you set this permission – Rohit Goswami Mar 09 '15 at 10:23
  • 1
    Ohh u did a mistake....if you are using Calander.Hour then it uses 12 hour pattern or if you are using HOUR_OF_DAY then 24 hour pattern...So changed to HOUR_OF_DAY...and this will work dear... – Rohit Goswami Mar 09 '15 at 10:26
  • @Devill: No, "inside on receive of myreceiver" is not displayed in my Log. My phone time is in IST. Calendar millis(14151): 1425896922656 & Alarm Manager interval(14151): 86400000. I changed the alarm setting time to 14:59 from 14:29 for testing. – Shreya Mar 09 '15 at 10:29
  • @RohitGoswami: I added that permission but it still does not work. – Shreya Mar 09 '15 at 10:30
  • u tried this ?....if you are using Calander.Hour then it uses 12 hour pattern or if you are using HOUR_OF_DAY then 24 hour pattern...So changed to HOUR_OF_DAY...and this will work dear... – Rohit Goswami Mar 09 '15 at 10:31
  • @RohitGoswami: I changed "HOUR" to "HOUR_OF_DAY" but it still doesn't work. – Shreya Mar 09 '15 at 10:33
  • @RohitGoswami Give a answer here... – Piyush Mar 09 '15 at 10:36
  • @RohitGoswami: I posted all the code that is required in my question. Please let me know if you figure out the problem. Thanks! – Shreya Mar 09 '15 at 10:40
  • remove this line and check again calendar.setTimeInMillis(System.currentTimeMillis()); – Rohit Goswami Mar 09 '15 at 10:41
  • @RohitGoswami: Tried it, not working yet. – Shreya Mar 09 '15 at 10:46
  • @Shreya Try with this `alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 30*1000, pendingIntent);`. And in which device u r running? – Piyush Mar 09 '15 at 10:48
  • please make a log and check up the calendars time with calendar.getTime()...is time correct? – Opiatefuchs Mar 09 '15 at 10:52
  • @Devill: I am running it on Samsung S3 Galaxy. I tried it with that line and it still does not work. – Shreya Mar 09 '15 at 10:52
  • Do you want to every day on 3:30 or any specific time ur service call? – Piyush Mar 09 '15 at 10:53
  • @Devill I actually want the service to start automatically every day at 9:00 AM even if the app is not open or background. – Shreya Mar 09 '15 at 10:55
  • It is working now guys! I do not know why but it suddenly started working. Thank you everyone for the help. Appreciate it! – Shreya Mar 09 '15 at 10:59
  • Try this...PendingIntent sender1=PendingIntent.getBroadcast(this, 100, myIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); – Rohit Goswami Mar 09 '15 at 11:01
  • @Shreya Great!! Enjoy! – Piyush Mar 09 '15 at 11:05

2 Answers2

17

Here is what I did to get it working:

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

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!

Shreya
  • 241
  • 1
  • 2
  • 11
  • 12
    com.android.alarm.permission.SET_ALARM is necessary only if you need to set the system wall clock time with setTime(). http://developer.android.com/reference/android/app/AlarmManager.html#setTime(long) – Nifhel Aug 04 '15 at 05:45
  • 1
    @Shreya. Its not working in my case even after adding that permission. In fact on Android 4.4 and lower, it does not fire at all. But on Android > 4.4, it does fire but not at the scheduled time – zulkarnain shah Jan 19 '18 at 06:17
0

Main Activity

public class MainActivity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (60 * 1000), pendingIntent); // 60 seconds after the current time
    }
}

Alarm Receiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        
        Log.e("Testing","ok");

    }

}

Manifests file

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.testapp">
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
          
           <receiver android:name=".MyBroadcastReceiver" 
              android:enabled="true" />
        </application>
    
    </manifest>

For background working add this in main activity(if application remove in to recent)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent();
            String packageName = getPackageName();
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
            }
        }