Как настроить планировщик блокировки и разблокировки на точное время в планшете lenovo - PullRequest
0 голосов
/ 12 января 2020

У меня есть класс BroadcastReceiver для установки планировщика для блокировки и разблокировки ... но он не работает в точно время для планшета lenovo (android 8.1) ... это работало с задержкой .... в чем моя проблема?

public class SchedulerAlarmReceiver extends WakefulBroadcastReceiver {

    AlarmManager mAlarmManager;
    PendingIntent mPendingIntent;
    public String EXTRA_SCHEDULER_ID = "Scheduler_ID";
    public String EXTRA_SCHEDULER_TYPE = "Scheduler_Type";
    public String EXTRA_SCHEDULER_TIME = "Scheduler_Time";

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("SCHEDULER", "SchedulerId : " + intent.getIntExtra(EXTRA_SCHEDULER_ID, 0));
        Log.i("SCHEDULER", "Scheduler_Type : " + intent.getStringExtra(EXTRA_SCHEDULER_TYPE));
        Log.i("SCHEDULER", "Scheduler_Time : " + intent.getLongExtra(EXTRA_SCHEDULER_TIME, 0));

        long schedulerTime = intent.getLongExtra(EXTRA_SCHEDULER_TIME, 0);
        int schedulerId = intent.getIntExtra(EXTRA_SCHEDULER_ID, 0);
        String schedulerType = intent.getStringExtra(EXTRA_SCHEDULER_TYPE);

        // SchedulerType checking for device lock and unlock
        switch (schedulerType) {

            // The device must be locked
            case SchedulerLockScreenViewModel.LOCK_TYPE:

                // Locking the device
                DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(DEVICE_POLICY_SERVICE);
                devicePolicyManager.lockNow();

                // Setting the scheduler for the next day
                saveNextDayScheduler(context, schedulerTime, schedulerId, schedulerType);

                break;
            case SchedulerLockScreenViewModel.UNLOCK_TYPE:

                // The device must be unlocked
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
                wl.acquire(10);


                // Setting the scheduler for the next day
                saveNextDayScheduler(context, schedulerTime, schedulerId, schedulerType);
                break;
        }
    }

    /**
     * Set alarms to lock and unlock the device
     *
     * @param context
     * @param calendar
     * @param ID
     */
    public void setAlarm(Context context, Calendar calendar, int ID, String type) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Calculate alarm time
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;
        long triggerTime = SystemClock.elapsedRealtime() + diffTime;

        // Put schedulerId and schedulerType and schedulerTime in intent extra
        Intent intent = new Intent(context, SchedulerAlarmReceiver.class);
        intent.putExtra(EXTRA_SCHEDULER_ID, ID);
        intent.putExtra(EXTRA_SCHEDULER_TYPE, type);
        intent.putExtra(EXTRA_SCHEDULER_TIME, calendar.getTimeInMillis());

        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Start the alarm with the selected time
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
                triggerTime,
                mPendingIntent);
    }

    /**
     * Cancellation of alarms with ID entered
     *
     * @param context
     * @param ID
     */
    public void cancelAlarm(Context context, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Cancellation Alarm using Scheduler ID
        mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, SchedulerAlarmReceiver.class), 0);
        mAlarmManager.cancel(mPendingIntent);
    }

    /**
     * Save scheduler time on alarms
     *
     * @param context
     * @param id
     */
    public void saveScheduler(Context context, long schedulerTime, int id, String type) {

        Calendar todayCal = Calendar.getInstance();
        todayCal.setTimeInMillis(System.currentTimeMillis()); // Setting the time of today in the calendar

        Calendar mCalendar = Calendar.getInstance();

        // Setting the clock according to the selected clock
        mCalendar.setTimeInMillis(schedulerTime);

        // Setting calendar date to today's date
        mCalendar.set(Calendar.YEAR, todayCal.get(Calendar.YEAR));
        mCalendar.set(Calendar.MONTH, todayCal.get(Calendar.MONTH));
        mCalendar.set(Calendar.DAY_OF_MONTH, todayCal.get(Calendar.DAY_OF_MONTH));

        //If the time has elapsed, it will set it for tomorrow
        if (todayCal.compareTo(mCalendar) == 1) {
            // Setting up the calendar for tomorrow
            mCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }

        // Cancel Alarm using Scheduler ID if alarm is exist
        cancelAlarm(context, id);

        // Set new Alarm using Scheduler ID
        setAlarm(context, mCalendar, id, type);

        Log.i("SCHEDULER", "Save scheduler " + (type.equals("LOCK") ? "lock" : "unlock") + " : \n " + mCalendar.getTime().toLocaleString());
    }

    /**
     * Saving scheduler for next day
     *
     * @param context
     * @param id
     */
    public void saveNextDayScheduler(Context context, long schedulerTime, int id, String type) {
        Calendar todayCal = Calendar.getInstance();
        todayCal.setTimeInMillis(System.currentTimeMillis()); // Setting the time of today in the calendar

        Calendar mCalendar = Calendar.getInstance();

        // Setting the clock according to the selected clock
        mCalendar.setTimeInMillis(schedulerTime);

        // Setting calendar date to today's date
        mCalendar.set(Calendar.YEAR, todayCal.get(Calendar.YEAR));
        mCalendar.set(Calendar.MONTH, todayCal.get(Calendar.MONTH));
        mCalendar.set(Calendar.DAY_OF_MONTH, todayCal.get(Calendar.DAY_OF_MONTH));
        mCalendar.add(Calendar.DAY_OF_YEAR, 1);
        // Cancel Alarm using Scheduler ID if alarm is exist
        cancelAlarm(context, id);

        // Set new Alarm using Scheduler ID
        setAlarm(context, mCalendar, id, type);

        Log.i("SCHEDULER", "Save Next day scheduler " + (type.equals("LOCK") ? "lock" : "unlock") + " : \n " + mCalendar.getTime().toLocaleString());
    }
}

В планшете WinTouah все в порядке ... блокировка и разблокировка одновременно ... но в lenovo это не нормально ... Я тестирую много кодов ... но я не могу решить свои проблема .. пожалуйста, помогите мне

...