Skip to content

Instantly share code, notes, and snippets.

@george-zhang-work
Created January 17, 2014 11:22
Show Gist options
  • Save george-zhang-work/8471878 to your computer and use it in GitHub Desktop.
Save george-zhang-work/8471878 to your computer and use it in GitHub Desktop.
This file is used to solve the miui wake up delay problem.
package com.dove.alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver {
public final static String TAG = AlarmReceiver.class.getSimpleName();
public final static String MANUFACTURER_XIAOMI = "xiaomi";
private final static long WAKEUP_DELAYED_TIME_MILLIS = 5 * 60 * 1000L;
private final static String ORIGIONAL_OPERATION = "origional_operation";
private final static String ORIGIONAL_SET_TIME = "origional_set_time";
public final static String ACTION_ALARM_RETRY = "com.dove.alarm.retry";
public final static String ACTION_ALARM_SEND = "com.dove.alarm.send";
public final static String ACTION_ALARM_CANCEL = "com.dove.alarm.cancel";
public static void set(Context context, int type, long triggerAtMillis, PendingIntent operation) {
PendingIntent sender = operation;
if (MANUFACTURER_XIAOMI.equalsIgnoreCase(Build.MANUFACTURER)) {
if (type == AlarmManager.RTC_WAKEUP || type == AlarmManager.ELAPSED_REALTIME_WAKEUP) {
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
intent.putExtra(ORIGIONAL_OPERATION, operation);
intent.putExtra(ORIGIONAL_SET_TIME, triggerAtMillis);
triggerAtMillis -= WAKEUP_DELAYED_TIME_MILLIS;
sender = PendingIntent
.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(type, triggerAtMillis, sender);
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_ALARM_RETRY.equals(action)) {
long triggerAtMillis = intent.getLongExtra(ORIGIONAL_SET_TIME,
System.currentTimeMillis());
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
PendingIntent operation = intent.getParcelableExtra(ORIGIONAL_OPERATION);
alarmManager.set(AlarmManager.RTC, triggerAtMillis, operation);
} else if (ACTION_ALARM_SEND.equals(action)) {
Log.d(TAG, "Receive a send action");
} else if (ACTION_ALARM_CANCEL.equals(action)) {
Log.d(TAG, " Receive a cancel action");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment