Hеllo guуs, I want to unlock the phone with service. I'm using alarm manager. Receiver and Service were added to manifest and also permissions for RECEIVE_BOOT_COMPLETE and WAKE_LOCK AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
setAlarms(context);
}
public static void setAlarms(Context context) {
cancelAlarms(context);
PendingIntent pIntent = createPendingIntent(context);
Calendar calendar = Calendar.getInstance();
setAlarm(context, calendar, pIntent);
}
@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
}
}
public static void cancelAlarms(Context context) {
PendingIntent pIntent = createPendingIntent(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
}
private static PendingIntent createPendingIntent(Context context) {
Intent intent = new Intent(context, AlarmService.class);
return PendingIntent.getService(context, 777, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
AlarmService
public class AlarmService extends Service {
public static String TAG = AlarmService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent alarmIntent = new Intent(getBaseContext(), MainActivity.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtras(intent);
getApplication().startActivity(alarmIntent);
//AlarmManagerHelper.setAlarms(this);
Log.e("SERVICE_WORKING","YEEEES!!!!");
return super.onStartCommand(intent, flags, startId);
}
}
MainActivity
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis()+10000, pendingIntent);
It works fine, but if i lock the screen it doesn't unlock it.
If you are simply looking for MainActivity to show when the phone is locked, there are Window flags that can be set in onCreate() to make that happen.
FLAG_SHOW_WHEN_LOCKED will enable your activity to be visible to the user when the phone is locked, and FLAG_DISMISS_KEYGUARD will unlock the phone IF the user has not configured a secure (pin/pattern/face/etc) keyguard.
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
You can only cause the screen to awaken via the alarm using a wakelock. You cannot unlock the screen programmatically. That would bypass the security the user has selected, which is not allowed.
Also, note that right now you are using a PendingIntent
with your alarm to start the Service
which in turn starts the Activity
. This is not guaranteed to work when the device is in a low power state. If you need to wake the device, use a PendingIntent
for a BroadcastReceiver
which in turn takes a wakelock and starts your Service
. The Service
can then take its appropriate action and release the wakelock. The system will ensure a wakelock is held long enough for your receiver's onReceive()
method to complete. It does not (and cannot) do the same for starting a Service
or Activity
from an alarm.
来源:https://stackoverflow.com/questions/28048439/wake-phone-from-unlock-screen-android