Discussion:
AlarmManager.RTC_WAKEUP Not Waking Device?
g1bb
2008-11-11 07:14:09 UTC
Permalink
Hello,

I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.

Also, this seems to be working fine in the emulator, but not my real
phone.

Here's the alarm scheduling:
ntent intent = new Intent(Main.this, OneShotAlarm.class);
PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);

// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 60);

// Schedule the alarm!
AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
The OneShotAlarm BroadCastReceiver:

public class OneShotAlarm extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();

intent = new Intent(context, ServiceClassToRun.class);

context.startService(intent);
}
---------------------------------
The Service:

public class ServiceClassToRun extends Service {
protected static final int Rnd = 0;

public void onStart(Intent intent, int startId) {
//do some business here
}
---------------------------------
And finally, my manifest:

<receiver android:name=".OneShotAlarm" android:process=":remote" />
<service android:name=".ServiceClassToRun "/>

Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.
hackbod
2008-11-11 10:51:10 UTC
Permalink
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.
g1bb
2008-11-11 15:53:36 UTC
Permalink
Interesting... I'll give that a shot. Thanks.
Post by hackbod
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.- Hide quoted text -
- Show quoted text -
g1bb
2008-11-11 17:45:11 UTC
Permalink
Another question on this:

Have you seen the Stopwatch application developed by Tom Taylor? I
just downloaded it, and it keeps counting after the phone is put to
sleep entirely. Any ideas on how he's doing that? I'm looking for that
kind of functionality.

Thanks again.
Post by hackbod
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.- Hide quoted text -
- Show quoted text -
g1bb
2008-11-11 20:02:22 UTC
Permalink
Maybe PARTIAL_WAKE_LOCK, looking at it now... interesting.
Post by g1bb
Have you seen the Stopwatch application developed by Tom Taylor? I
just downloaded it, and it keeps counting after the phone is put to
sleep entirely. Any ideas on how he's doing that? I'm looking for that
kind of functionality.
Thanks again.
Post by hackbod
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
hackbod
2008-11-11 21:38:34 UTC
Permalink
Yes, for both these things, you need to hold a partial wake lock to
keep the phone from turning off.
Post by g1bb
Maybe PARTIAL_WAKE_LOCK, looking at it now... interesting.
Post by g1bb
Have you seen the Stopwatch application developed by Tom Taylor? I
just downloaded it, and it keeps counting after the phone is put to
sleep entirely. Any ideas on how he's doing that? I'm looking for that
kind of functionality.
Thanks again.
Post by hackbod
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
g1bb
2008-11-12 18:25:16 UTC
Permalink
Thanks hackbod, that worked great.

One more question though: I'm acquiring my wakelock in an activity, as
well as attempting to release it in the OnDestroy() function of the
activity itself. Is there a good way to destroy the activity
programmatically? It doesn't appear to be done automatically from what
I could tell, unless I use Process.Kill();.

Thanks again!
Post by hackbod
Yes, for both these things, you need to hold a partial wake lock to
keep the phone from turning off.
Post by g1bb
Maybe PARTIAL_WAKE_LOCK, looking at it now... interesting.
Post by g1bb
Have you seen the Stopwatch application developed by Tom Taylor? I
just downloaded it, and it keeps counting after the phone is put to
sleep entirely. Any ideas on how he's doing that? I'm looking for that
kind of functionality.
Thanks again.
Post by hackbod
It does wake it up, but you are going to need to hold a wake lock to
keep the device awake after your intent receiver returns.
Post by g1bb
Hello,
I've been trying to use AlarmManager.RTC_WAKEUP to wake up my device
while it's sleeping, and then play some sounds. It seems like when
it's charging, this works fine, but when it's not, the service doesn't
start until I hit the menu button and am presented with the lock
screen.
Also, this seems to be working fine in the emulator, but not my real
phone.
                  ntent intent = new Intent(Main.this, OneShotAlarm.class);
                  PendingIntent sender =
PendingIntent.getBroadcast(Main.this, 0, intent, 0);
                    // We want the alarm to go off 30 seconds from now.
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, 60);
                    // Schedule the alarm!
                    AlarmManager am =
(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
---------------------------------
public class OneShotAlarm extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "OneShotAlarm Broadcast invoked",
1000).show();
          intent = new Intent(context, ServiceClassToRun.class);
         context.startService(intent);
     }
---------------------------------
public class ServiceClassToRun extends Service {
        protected static final int Rnd = 0;
         public void onStart(Intent intent, int startId) {
         //do some business here
     }
---------------------------------
 <receiver android:name=".OneShotAlarm" android:process=":remote" />
 <service android:name=".ServiceClassToRun "/>
Any ideas are greatly appreciated. Has anyone else experienced this as
well? Thanks in advance.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Mark Murphy
2008-11-12 18:32:51 UTC
Permalink
Is there a good way to destroy the activity programmatically?
Call finish(). Once you return control back to Android (e.g., leave the
on...() callback you're in), your activity should be gracefully shut
down, including having onDestroy() called.
--
Mark Murphy (a Commons Guy)
http://commonsware.com

Android Training on the Ranch! -- Mar 16-20, 2009
http://www.bignerdranch.com/schedule.shtml
g1bb
2008-11-12 19:45:10 UTC
Permalink
Hi Mark,

Sorry, I forgot to mention that I would like to destroy the activity
from a different class not associated with the activity. Is that
possible?

Thanks again.
Post by Mark Murphy
Is there a good way to destroy the activity programmatically?
Call finish(). Once you return control back to Android (e.g., leave the
on...() callback you're in), your activity should be gracefully shut
down, including having onDestroy() called.
--
Mark Murphy (a Commons Guy)http://commonsware.com
Android Training on the Ranch! -- Mar 16-20, 2009http://www.bignerdranch.com/schedule.shtml
Mark Murphy
2008-11-12 19:51:38 UTC
Permalink
Post by g1bb
Hi Mark,
Sorry, I forgot to mention that I would like to destroy the activity
from a different class not associated with the activity. Is that
possible?
Provide that class with a reference to your Activity when you create it.

E.g.:

class ActivityKiller {
ActivityKiller(Activity victim) {
this.victim=victim;
}

void pussycatKillKill() {
victim.finish();
}
}

You will also need to arrange for background threads to close up shop,
including the thread your alternate class' code is running on, if that's
not the main UI thread.
--
Mark Murphy (a Commons Guy)
http://commonsware.com

Android Training on the Ranch! -- Mar 16-20, 2009
http://www.bignerdranch.com/schedule.shtml
g1bb
2008-11-13 05:25:05 UTC
Permalink
Thanks Mark. I went about this another way, as I forgot to mention
that I was going from an activity, to a broadcastreceiver, to a class.
I ended up calling the same activity with the FLAG_ACTIVITY_SINGLE_TOP
and FLAG_ACTIVITY_NEW_TASK flags, and then calling the finish() in the
onNewIntent event of the intent.
Post by Mark Murphy
Post by g1bb
Hi Mark,
Sorry, I forgot to mention that I would like to destroy the activity
from a different class not associated with the activity. Is that
possible?
Provide that class with a reference to your Activity when you create it.
class ActivityKiller {
        ActivityKiller(Activity victim) {
                this.victim=victim;
        }
        void pussycatKillKill() {
                victim.finish();
        }
}
You will also need to arrange for background threads to close up shop,
including the thread your alternate class' code is running on, if that's
not the main UI thread.
--
Mark Murphy (a Commons Guy)http://commonsware.com
Android Training on the Ranch! -- Mar 16-20, 2009http://www.bignerdranch.com/schedule.shtml
Loading...