Wake the device up when app prompts user

后端 未结 3 969
挽巷
挽巷 2020-12-18 12:15

I\'m using handler to repeatedly prompt user for an input every e.g. 5 minutes. When the device goes into sleeping mode and screen is locked, how can I wake the device up wh

3条回答
  •  一向
    一向 (楼主)
    2020-12-18 12:44

    Take a look for AlarmManager class http://developer.android.com/reference/android/app/AlarmManager.html It's something like "cron"

    Ok, here is a piece of working code - first comes the activity class:

    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.PowerManager;
    import android.os.PowerManager.WakeLock;
    
    public class MainActivity extends Activity {
        private WakeLock wl;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //acquire wake lock
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "TAG");
            wl.acquire();
    
            // schedule alarm
            Intent i = new Intent();
            i.setAction(WakeReciever.WAKE_INTENT);
            PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, i, 0);
    
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000,
                    pIntent);
        }
    
        @Override
        protected void onPause() {
            wl.release();
            super.onPause();
        }
    
    }
    

    Next comes BroadcastReceiver:

    package com.test;
    
    import android.content.BroadcastReceiver;
    import android.content.Intent;
    import android.content.Context;
    import android.os.PowerManager;
    import android.os.PowerManager.WakeLock;
    
    public class WakeReciever extends BroadcastReceiver {
        public static final String WAKE_INTENT = "com.test.WAKE"; 
    
    
        /**
         * @see android.content.BroadcastReceiver#onReceive(Context,Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            //acquire wake lock
    
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
            wl.acquire();
    
    
            //start activity
            Intent i = new Intent();
            i.setClassName("com.test", "com.test.MainActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            context.startActivity(i);
    
            wl.release();
        }
    }
    

    And finally don't forget about the manifest file:

    
    
        
        
        
            
                
                    
                    
                
            
            
                
                    
                
            
    
        
    
    

提交回复
热议问题