سؤال

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>
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top