0

I am creating an alarm for my app. The alarm is suppose to wake the app up at around 8 in the morning and make a server call. When I change the time to say an hour or less, it is able to fetch the data and give the notifications. But when I extend the Calendar to a day interval, it doesn't work. This is my code.

Setting Alarm:

private void SetAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 9);

    Intent intent = new Intent(this, TodayWordReceiver.class);
    boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(),
            0, intent, PendingIntent.FLAG_NO_CREATE) != null);
    if (!alarmUp) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

        Log.i("Alarm", "alarm set success");
    } else {
        Log.i("Alarm", "Already created");
    }
}

TodayWordReceiver:

public class TodayWordReceiver extends BroadcastReceiver {
public TodayWordReceiver() {}

@Override
public void onReceive (Context context, Intent intent) {
    WakeLocker.acquire(context);
    Log.i("Receiver", "Called here");
    Intent serIntent1 = new Intent(context, FetchTodayWordService.class);
    context.startService(serIntent1);

}

}

FetchTodayService:

public class FetchTodayWordService extends IntentService {

WordDay wordDay;
final String url =
        "http://api.wordnik.com:80/v4/words.json/wordOfTheDay?api_key=KEY";
public FetchTodayWordService () {
    super("FetchTodayWordService");
}

@Override
protected void onHandleIntent (Intent intent) {
    Log.i("OUTPUT", "Service called");
    MakeRequest();
}
public void MakeRequest()
{
    JsonObjectRequest jsonObjectRequest =
            new JsonObjectRequest
                    (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse (JSONObject response) {
                            wordDay = ParseWordDay.ParseJSON(response);
                            if(wordDay != null)
                            {
                                new StoreData().DoWork(getApplicationContext(), wordDay);
                            }

                        }
                    },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse (VolleyError error) {
                                    Log.i("OUTPUT", String.valueOf(error));


                                }
                            });
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}

WakeLocker:

public class WakeLocker {
private static PowerManager.WakeLock wakeLock;

public static void acquire(Context ctx) {
    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "LIGHT APP");
    wakeLock.acquire();
}

public static void release() {
    if (wakeLock != null) wakeLock.release(); wakeLock = null;
}

}

Manifest Permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Thanks Mike for the reply. If you will check the BroadcatReceiver, I have implemented a custom class from which i gain a WakeLock. – Clinton Yeboah Apr 18 '16 at 09:41
  • Oops, yeah, I see it now. Sorry 'bout that. Missed it the first time through. Are you sure it's working? Do you have the permission, and everything? – Mike M. Apr 18 '16 at 09:43
  • I think so Mike, I see the phone come on when it wants to fire. And you can check the permissions. Have edited the post – Clinton Yeboah Apr 18 '16 at 09:54
  • At first, I thought it was because I was using a custom ROM, so I downloaded an alarm app from github. And it worked!. And I have other alarms on my phone too – Clinton Yeboah Apr 18 '16 at 09:55
  • Well, everything looks OK, at a glance. I would mention that `setInexactRepeating()` is, obviously, inexact, and with an interval of a day, it could be off by quite a bit. You might use `setExact()` instead, at least while you're debugging. – Mike M. Apr 18 '16 at 10:32
  • Hi Mike, whiles I was trying to figure out the problem, I tried that. I didn't make any difference. :( – Clinton Yeboah Apr 18 '16 at 10:41
  • Mike, you see I have inserted some Log functions. After a day, I insert my phone and check for the Log outputs. Sometimes, the service is called, but the volley throws a "networkerror" exception. So I tried something else, I created another alarm that will fire in 30 minutes if the first alarm gives an error. When this alarm is succeses, it cancels itself from firing again. But, this second alarm also kept on throwing "networkerror exception" until I turn the app on. Isn't it weird? – Clinton Yeboah Apr 18 '16 at 10:47
  • Are you testing this under Marshmallow, by chance? I'm not too familiar with that version yet, but I know the new doze mode disallows network access. – Mike M. Apr 18 '16 at 10:53
  • No, is Kitkat. Android 4.4.4 – Clinton Yeboah Apr 18 '16 at 11:16
  • This is something I have being working on for a month. The very core of my app is alarms. So I wanted to know more about it. I created another project for the alarm feature only. I wasn't making any network call. It was just displaying a notification. It behaved the same way. It won't display, till I turn the app on. – Clinton Yeboah Apr 18 '16 at 11:30
  • Hi Mike, I finally found the solution. It wasnt easy. I am using an MIUI Rom. And apps are not allowed to restart themselves by default. You need to explicitly allow them to. This answer helped me. I was testing with a Xiaomi. http://stackoverflow.com/questions/26615633/how-to-auto-restart-a-service-when-killed – Clinton Yeboah Apr 29 '16 at 14:37

0 Answers0