PowerManager wakelock не выводит устройство из строя
У меня есть приложение, в котором фоновая служба работает каждую минуту. Я хочу, чтобы служба разбудила устройство, если оно спит. Я использую PowerManager, но устройство не просыпается. Есть идеи почему? Заранее спасибо.
@Override
protected void onHandleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"My Tag");
wl.acquire();
// do work as device is awake
wl.release();
}
[Edit1]
Вот так я запускаю сервис из Активности.
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra("alarm_message", "sending outstanding transactions");
// In reality, you would want to have a static variable for the
// request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(
getApplicationContext(), 192837, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
// 86400000 = 24 hours
// 43200000 = 12 hours
// 3600000 = 1hr
// 1800000 = 30 mins
// 300000 = 5 mins
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),60000,sender);
Класс AlarmReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
// Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(context,
SendOutstandingTransactions.class);
myIntent.setAction("com.carefreegroup.startatboot.MyService");
context.startService(myIntent);
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
Следующий класс вызывает Activity, которую я хочу запустить, когда устройство спит.
SendOutstandingTransactions IntentService.
protected void onHandleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"My Tag");
wl.acquire();
if (hasMessageDisplayed == false) {
Intent i = new Intent(this, DisplayMessageActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
wl.release();
}
[Edit2]
@Override
public void onDestroy() {
super.onDestroy();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "com.something.alarm");
// Acquire the lock
if (wl.isHeld()) wl.release();
}
2 ответа
Либо используйте WakefulIntentService от Commonsware, либо сделайте следующее:
class YourService extends IntentService {
private static final String LOCK_NAME = YourService.class.getName()
+ ".Lock";
private static volatile WakeLock lockStatic = null; // notice static
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
public static void startYourService(Context ctxt, Intent i) { // STATIC
getLock(ctxt.getApplicationContext()).acquire();
ctxt.startService(i);
}
public YourService(String name) {
super(name);
setIntentRedelivery(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0) {
lock.acquire();
}
super.onStartCommand(intent, flags, startId);
return (START_REDELIVER_INTENT);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
// do your thing
} finally {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (lock.isHeld()) lock.release();
}
}
}
и в вашем приемнике:
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
// Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
myIntent.setAction("com.carefreegroup.startatboot.MyService");
YourService.startYourService(context, myIntent)
который на самом деле является ядром @CommonsWare WakefulIntentService (не уверен насчет START_FLAG_REDELIVERY, я должен спросить на днях)
Вам нужно использовать AlarmManager, чтобы получить блокировку, активировать службу в определенный момент времени и позволить службе разблокировать блокировку. Сам сервис, даже если он работает в фоновом режиме, не может быть вызван, потому что процессор уже спит и не будет выполнять ваш код.
Поместите это в onReceive вашего AlarmManager:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.something.alarm");
//Acquire the lock
System.out.println("+++ Acquiring Lock +++");
if(!wl.isHeld())
wl.acquire();
// Fire your service
Затем отпустите wakelock в конце выполнения вашего сервиса. Это означает, что вы должны вызвать wl.release() внутри вашего сервиса, прежде чем он закончится.