Android: can Android AlarmManager wake up the device to perform the scheduled task?

限于喜欢 提交于 2019-12-18 12:43:19

问题


I need to make an App that set up a calendar for the week,

basically the user will input the start hour and finish hour of the activity for the every day of the week.

He will do only once.

After that set up, the application (I will use AlarmManager) will start his activity (continuously play video) at the set hour and finish and the set hour:

Every day of the week;

forever,

without human interaction (of course the phone/tablet must be switched on and plugged to electricity).

My concern is the following:

Will the alarmmanger be able to actually wake up the device in the morning to start the activity (play the video) without any interaction from the user?

The clever suggestion of using

    WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);
    Window window = getWindow();  
    window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);

Gives me a lot of errors in Eclipse:


回答1:


Yes u can i tried something similar to that but not exactly..i tried invoking the device every day at 9.00AM to download the contents i used this piece of code

PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    wl.release();

This was my implementation: Used to set the Alaram

 AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(System.currentTimeMillis());
     calendar.add(Calendar.MINUTE, 10);
     calendar.add(Calendar.SECOND, 00);
     //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*1, pi);

BroadcastReciever:

Register BroadcastReciever:
PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();
    // Put here YOUR code.

    Intent startAutoSyncService = new Intent(context,
            AppoinmentService.class);
    context.startService(startAutoSyncService);

    wl.release();



回答2:


yes alarm is gd option. use keyguard dismiss if the device is locked ..

 Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

at the time of invking alarm & starting you video/audio activity or screen which you wants to be at the time of alarm get invoked

also on device reboot you need to reset the alarms




回答3:


Yes, it will, if you use ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP as the alarm type.



来源:https://stackoverflow.com/questions/17543784/android-can-android-alarmmanager-wake-up-the-device-to-perform-the-scheduled-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!