Alarm: handle wakelock properly

This commit is contained in:
Snek 2017-03-25 01:10:50 +03:00
parent 3bba6d0244
commit 151a2bd510
3 changed files with 33 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.PowerManager;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
@ -22,9 +23,12 @@ class Alarm {
private static final String TAG = "Alarm"; private static final String TAG = "Alarm";
private static boolean isPlaying = false; private static boolean isPlaying = false;
private static PowerManager.WakeLock sCpuWakeLock;
static void start(Context context, int ringtone) { static void start(Context context, int ringtone) {
acquireCpuWakeLock(context);
settings = PreferenceManager.getDefaultSharedPreferences(context); settings = PreferenceManager.getDefaultSharedPreferences(context);
if (settings.getBoolean("vibrate", false)) { if (settings.getBoolean("vibrate", false)) {
@ -61,11 +65,11 @@ class Alarm {
m.release(); m.release();
notificationManager.cancel(ALARM_NOTIFICATION_ID); notificationManager.cancel(ALARM_NOTIFICATION_ID);
alarmManager.cancel(pendingIntent); alarmManager.cancel(pendingIntent);
releaseCpuLock();
isPlaying = false; isPlaying = false;
} if (v != null) {
v.cancel();
if (v != null) { }
v.cancel();
} }
Log.d(TAG, "Cancel"); Log.d(TAG, "Cancel");
@ -76,4 +80,25 @@ class Alarm {
return isPlaying; return isPlaying;
} }
private static void acquireCpuWakeLock(Context context) {
if (sCpuWakeLock != null) {
return;
}
PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, TAG);
sCpuWakeLock.acquire();
}
private static void releaseCpuLock() {
if (sCpuWakeLock != null) {
sCpuWakeLock.release();
sCpuWakeLock = null;
}
}
} }

View File

@ -1,6 +1,7 @@
package com.example.yink.amadeus; package com.example.yink.amadeus;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -9,7 +10,7 @@ import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver; import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
public class AlarmReceiver extends WakefulBroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
private final String TAG = "AlarmReceiver"; private final String TAG = "AlarmReceiver";
@ -32,9 +33,8 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {
Alarm.start(context, ringtones[index]); Alarm.start(context, ringtones[index]);
ComponentName comp = new ComponentName(context.getPackageName(), Intent service = new Intent(context, AlarmService.class);
AlarmService.class.getName()); context.startService(service);
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK); setResultCode(Activity.RESULT_OK);
} }

View File

@ -19,7 +19,6 @@ public class AlarmService extends IntentService {
Intent launch = new Intent(this, LaunchActivity.class); Intent launch = new Intent(this, LaunchActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch); startActivity(launch);
AlarmReceiver.completeWakefulIntent(intent);
} }
private void sendNotification(String msg) { private void sendNotification(String msg) {