3

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.

Jenya Kirmiza
  • 511
  • 8
  • 21
  • 3
    I didn't catch, what do you mean by unlocking phone? You would like to unlock phone even if it has password or pin for unlock – and move from device sleep to device is on a screen with running launcher state? So, in this case this is impossible, I think. – krossovochkin Jan 20 '15 at 15:00
  • sorry, i meant unlocking the screen – Jenya Kirmiza Jan 20 '15 at 15:19

2 Answers2

2

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);
Jim Vitek
  • 4,174
  • 3
  • 23
  • 32
  • I want to start rington and alert the user like alarm clock – Jenya Kirmiza Jan 20 '15 at 15:30
  • I used also flags FLAG_TURN_SCREEN_ON and FLAG_ALLOW_LOCK_WHILE_SCREEN_ON. It worked, but i saw other people used PowerManager? is it right tto use PowerManager? – Jenya Kirmiza Jan 20 '15 at 15:37
  • There were many lollipop changes to powermanager & wakelocks. You may want to take a look at AlarmActivity.java (standard android alarm clock) to see how they do it - https://android.googlesource.com/platform/packages/apps/DeskClock/+/dfd1960/src/com/android/deskclock/alarms/AlarmActivity.java – Jim Vitek Jan 21 '15 at 02:51
0

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.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • thanks, You mean i shouldn't start Service from Pending Intent, but start BroadcastReceiver and then start service from receiver? – Jenya Kirmiza Jan 20 '15 at 15:24
  • Yes. You'll also need to ensure the `BroadcastReceiver` takes a wakelock and the `Service` releases it when it is done doing whatever it does on wake up. Otherwise there is a chance the system will go back to sleep before the `Service` ever gets to run. – Larry Schiefer Jan 20 '15 at 15:30
  • I need to use PowerManager to ackqure wake lock? – Jenya Kirmiza Jan 20 '15 at 15:39
  • Yes. Alternatively, you can derive your receiver from `WakefulBroadcastReceiver` to help with this. It wraps the calls to the `PowerManager` in order to make it easier to deal with. – Larry Schiefer Jan 20 '15 at 16:12