0

this is my code for setting alarm:

public void SetAlarm(Context context, int tag, long time){
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     i.putExtra("position", tag);
     PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
     am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute
}

this is my onRecieve methode:

public void onReceive(final Context context, Intent intent){   
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wak up lo");
    wl.acquire();

    position = intent.getIntExtra("position", -1);         
    new PostManager(context, position);

    wl.release();
}

this is working well with emulator. at the same time i set an alarm which will trigger after 24 hour in emulator and real device. the work is done by the emulator well, but not in real device. is this happening for PowerManager.FULL_WAKE_LOCK or anything else? i have tried hard but failed find any solution.

Shoshi
  • 2,254
  • 1
  • 29
  • 43
  • Are you absolutely sure that the value of your variable `time` is correct in the `setAlarm()` method? Add some debug logging and check your logcat to ensure that the alarm is actually being set for the time you think it should be. – David Wasser Mar 04 '13 at 09:12
  • @DavidWasser : `time` is correct because, at the same time i have set alarm in emulator and real device. the alarm should trigger after 4 hour. in emulator it triggered, but not in real device. in real device alarm work well if i set the alarm less than 4 hour. i think my alarm is unable to wake up my device from sleep. – Shoshi Mar 04 '13 at 09:54
  • i have also tried `PowerManager.PARTIAL_WAKE_LOCK` instead of `PowerManager.FULL_WAKE_LOCK`, but it doesn't work – Shoshi Mar 04 '13 at 09:55
  • What device are you using? – David Wasser Mar 04 '13 at 10:20
  • What does `new PostManager(context, position)` do? If this is long-running, you need to put it in a service and start your service from the broadcast receiver. You should also add logging to your `onReceive()` method and look at the logcat after 4 hours. You can then determine if your `onReceive()` method is being called. Yous don't need to get a wake lock in the `onReceive()` method because Android will keep the phone awake until that method completes (unless it takes too long in which case Android will just kill it). – David Wasser Mar 04 '13 at 10:25
  • i am using nexus 7. and yes `new PostManager(context, position)` may take highest 30s. if it may killed by android, then why it is working well in emuletor ? i am just confused. in `new PostManager(context, position)` there is a internet connection working in a asynctask. @DavidWasser – Shoshi Mar 04 '13 at 10:54
  • I can't tell you why it works in the emulator. Obviously the rules are a bit different there. You can't make an Internet connection in `onReceive()`, this method must return immediately. So you need to move this to a service and have your broadcast receiver start the service. You'll need to get a wakelock in the broadcast receiver to ensure that the phone stays awake long enough to start the service before going back to sleep. You might want to have a look at http://stackoverflow.com/questions/7182002/correct-pattern-to-acquire-a-wakelock-in-a-broadcastreceiver-and-release-it-in-a – David Wasser Mar 04 '13 at 11:02
  • @DavidWasser : i have used `Log.e`. after 4 hour i see that the `onReceive` is called. so i think i should use `service` in `onReceive` rather than a simple class. i will try and let u know what happen. thankx – Shoshi Mar 04 '13 at 11:59
  • sorry pal. i am so stupid. the error was in `postmanager()`. i am really sorry for wasting your time. but i have also used `service` as u have suggested to avoid the future error. thankx @DavidWasser – Shoshi Mar 04 '13 at 23:39
  • No problem. Please create an answer for your own question and accept it so that the question doesn't stay on the "unanswered" list. – David Wasser Mar 05 '13 at 10:01

1 Answers1

0

sorry guys, the problem was in PostManager. in PostManager, i was checking a session cookie with another string. so what happen? here is the answwer. before 4 hour the session cookie remain on cookiemanager so the checking by .equal(another_string) work fine. but i think after 4 hours the session cookie expire and .equal(another_string) throw a NullPointerException. so i have just check that the cookie i have get, is null or not. this solve my problem. again sorry guys.

Shoshi
  • 2,254
  • 1
  • 29
  • 43