我使用WakefulBroadcastReceiver
中的方法设置AlarmManager
public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {
public static final String WAKEUP = "wakeup";
@Override
public void onReceive(Context context, Intent intent) {
Utils.PrintInfo("Alarm received!");
try {
Intent newIntent = new Intent(context, AlarmWakefulService.class);
startWakefulService(context, newIntent);
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void setOnetimeTimer(Context context, long triggerAtMillis) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(WAKEUP, true);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pi);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pi);
}
}
和我的AlarmWakefulService
public class AlarmWakefulService extends IntentService {
public AlarmWakefulService() {
super("AlarmWakefulService");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "WakeLockLauncher");
wakeLock.acquire();
Utils.PrintInfo("AlarmManagerBroadcastReceiver, Completed service @ "
+ SystemClock.elapsedRealtime());
Intent newIntent = new Intent(context, AlarmReceiveActivity.class);
newIntent.putExtra("ALARM_TIME", intent.getStringExtra("ALARM_TIME"));
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
wakeLock.release();
wakeLock = null;
AlarmManagerBroadcastReceiver.completeWakefulIntent(intent);
}
}
两者都在清单文件
中 <receiver android:name="com.xxxx.alarm.AlarmManagerBroadcastReceiver" ></receiver>
<service android:name="com.xxxx.alarm.AlarmWakefulService" />
我也得到了许可
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
并不总是闹钟唤醒设备,有时我必须解锁设备才能进行闹钟呼叫,我也尝试使用ELAPSED_REALTIME_WAKEUP
而不是RTC_WAKEUP
设置它(当我将RTC_WAKEUP
更改为{时{1}}我改变了计算ELAPSED_REALTIME_WAKEUP
)的方法,但后来甚至更糟,设备没有更频繁地唤醒。
还有其他方法可以将设备从深度睡眠中唤醒吗?
答案 0 :(得分:-1)
您的接收器需要在远程进程上运行。这允许系统唤醒该过程。在你的清单中使用它:
<receiver android:process=":remote" android:name="com.maxcom.alarm.AlarmManagerBroadcastReceiver" />
我刚刚向接收者添加了android:process=":remote"
声明。