Alarm: handle wakelock properly
This commit is contained in:
parent
3bba6d0244
commit
151a2bd510
|
@ -7,6 +7,7 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.PowerManager;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
@ -22,9 +23,12 @@ class Alarm {
|
|||
|
||||
private static final String TAG = "Alarm";
|
||||
private static boolean isPlaying = false;
|
||||
private static PowerManager.WakeLock sCpuWakeLock;
|
||||
|
||||
static void start(Context context, int ringtone) {
|
||||
|
||||
acquireCpuWakeLock(context);
|
||||
|
||||
settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
if (settings.getBoolean("vibrate", false)) {
|
||||
|
@ -61,11 +65,11 @@ class Alarm {
|
|||
m.release();
|
||||
notificationManager.cancel(ALARM_NOTIFICATION_ID);
|
||||
alarmManager.cancel(pendingIntent);
|
||||
releaseCpuLock();
|
||||
isPlaying = false;
|
||||
}
|
||||
|
||||
if (v != null) {
|
||||
v.cancel();
|
||||
if (v != null) {
|
||||
v.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(TAG, "Cancel");
|
||||
|
@ -76,4 +80,25 @@ class Alarm {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.example.yink.amadeus;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
@ -9,7 +10,7 @@ import android.preference.PreferenceManager;
|
|||
import android.support.v4.content.WakefulBroadcastReceiver;
|
||||
import android.util.Log;
|
||||
|
||||
public class AlarmReceiver extends WakefulBroadcastReceiver {
|
||||
public class AlarmReceiver extends BroadcastReceiver {
|
||||
|
||||
private final String TAG = "AlarmReceiver";
|
||||
|
||||
|
@ -32,9 +33,8 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {
|
|||
|
||||
Alarm.start(context, ringtones[index]);
|
||||
|
||||
ComponentName comp = new ComponentName(context.getPackageName(),
|
||||
AlarmService.class.getName());
|
||||
startWakefulService(context, (intent.setComponent(comp)));
|
||||
Intent service = new Intent(context, AlarmService.class);
|
||||
context.startService(service);
|
||||
setResultCode(Activity.RESULT_OK);
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ public class AlarmService extends IntentService {
|
|||
Intent launch = new Intent(this, LaunchActivity.class);
|
||||
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(launch);
|
||||
AlarmReceiver.completeWakefulIntent(intent);
|
||||
}
|
||||
|
||||
private void sendNotification(String msg) {
|
||||
|
|
Loading…
Reference in New Issue