0

I have a app that takes care of certain silence functions, but I can't make it work when the phone is sleeping and not connected to the computer. Everything works fine when the screen is on, but it stops working when the phone sleeps. Any idea what I'm doing wrong? I have checked 100 other posts here, but I just can't identify my problem.

My Alarm manager that makes the intent:

Intent intent2 = new Intent(this, Silence.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1000 + id, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am2 = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent2);

Broadcast Receiver:

public class MainSilenceCatcherVibrate extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent1) {
    WakeLocker.acquire(context);
    System.out.println("test");
    WakeLocker.release();
    }
}

Manifest usage:

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<receiver android:name=".Silence"></receiver>
<activity android:name=".ContactActivity"></activity>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Reaver
  • 323
  • 3
  • 21

1 Answers1

0

i just cant identify my problem

Your event will happen once immediately, then not again for a week (AlarmManager.INTERVAL_DAY * 7,). Perhaps you did not wait a week.

Note that you do not need WakeLocker here -- so long as all your work is in onReceive(), the system will hold a WakeLock for you. If you have work that will take more than a millisecond or so, you will not want to do that in onReceive(), but in that case I would recommend WakefulBroadcastReceiver or my WakefulIntentService over rolling your own WakeLock solution.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I went into my app and specified that it should fire 1 min from "now" (relative to when i test). And when the screen is on or i am connected for debugging then there is no problem. The problem comes when i unhook myself from the computer (so that it actually sleeps) as it is suposed to take me into silent mode instead of printing something. It takes my into silent mode when i turn the screen on manually and not before (and even if i use PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP the screen does not light up when its not connected to the computer). – Reaver Oct 12 '13 at 12:40
  • @user2523881: Use **`adb shell dumpsys alarm`** to ensure that your alarm is configured correctly. If you are on a SONY Xperia that offers STAMINA Mode, either disable that or add your app to the whitelist. – CommonsWare Oct 12 '13 at 12:42
  • Thanks for the tip about STAMINA mode. As it turns out, i got a Sony Xperia and the battery optimization of Xperia makes my app unable to recieve the broadcast. – Reaver Oct 12 '13 at 13:54
  • @user2523881: If it is STAMINA mode, the problem is that `RTC_WAKEUP` is treated equivalently to `RTC`: http://commonsware.com/blog/2013/03/08/warning-xperia-z-stamina-alarmmanager.html http://commonsware.com/blog/2013/03/11/more-xperia-alarmmanager.html – CommonsWare Oct 12 '13 at 13:59
  • Thanks a bunch for the links, i can now see why it would not work after so many frustrating hours of thinking it was my programs fault. – Reaver Oct 12 '13 at 14:09