0

I have a broadcast receiver who's job is to open an Activity. (Specifically when i receive a certain SMS). When my phone is on and I use it, it works great.
But when my phone is on sleep mode nothing happens.
Do I need to add a code in my broadcast receiver to WAKEUP the phone and release the SCREEN LOCK (if there is) and then open the Activity?
Or is there a better solution?

This is the relevant code in the broadcast receiver onReceive function:

    //Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(context, Alarm.class);
intent.putExtra("coordinates", coordinates);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
    0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 100, pendingIntent);
Kara
  • 6,115
  • 16
  • 50
  • 57
  • Have you read this? http://stackoverflow.com/questions/8073631/android-wake-phone – Amokrane Chentir Oct 21 '12 at 12:49
  • Yes, But maybe there was another solution. How do i check if i need to do it or not? (If the phone is on sleep or not?) – Erel Hansav Oct 21 '12 at 13:30
  • The point is, if you use a wake lock, the phone will perform it's action regardless of if the phone is on or not. It is specifically designed to wake the phone from a sleep state, and perform your action (so you don't check - you just know it will be performed, regardless of the phones state) – Booger Oct 22 '12 at 18:23

1 Answers1

1

You will need to use a WakeLock to accomplish this.

This particular tutorial helped me with this: http://it-ride.blogspot.com/2010/10/android-implementing-notification.html

and another better tutorial specifically about wakelocks: https://github.com/commonsguy/cwac-wakeful

Booger
  • 18,579
  • 7
  • 55
  • 72
  • How do i check if i need to do it or not? (If the phone is on sleep or not?) – Erel Hansav Oct 21 '12 at 13:31
  • 1
    If you want to make sure something happens, regardless of if the phone is on or not - you will use a wake lock. This ensures your action is performed regardless of if the phone is awake or not (so you won't need to check). I added the link to another helpful tutorial I used. – Booger Oct 21 '12 at 13:59