首页 > 代码库 > 锁屏状态下点亮屏幕,并弹出闹钟提示信息

锁屏状态下点亮屏幕,并弹出闹钟提示信息

锁屏状态下点亮屏幕,并弹出闹钟提示信息,可以在锁屏界面上取消闹钟;使用广播接收闹钟定时:


下面是例子里的核心代码如下


android 设置定时闹钟(包括提醒一次和循环提醒):
Intent intent = new Intent(MainActivity.this,MyAlarmBroadCast.class);  
        
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);  
          
          
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
      
          
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);  
              
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);  

android取消指定闹钟:
Intent intent = new Intent(AlarmReminderActivity.this,MyAlarmBroadCast.class);  

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);  
          
          
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        
        alarmManager.cancel(pendingIntent);

android唤醒锁定屏幕:
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
        mWakelock.acquire();

该方法的使用一定要伴随mWakelock.release();否则会报异常;
推荐使用方法:在唤醒屏幕显示的activity的onResume方法中唤醒,在onPause方法中release;


如何让activity显示在锁屏界面上:
首先要唤醒锁定屏幕,然后设置activity的属性,使它可以显示在锁屏界面上;
唤醒屏幕的方法参考上面;
activity的设置包括两部分:
1-onCreate方法中设置如下代码:
super.onCreate(savedInstanceState);
final 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);
setContentView(R.layout.act_alarmreminder);
2-AndroidManifest.xml对该activity的声明中设置属性:
android:label="@string/app_name" 
            android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:taskAffinity=""
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"(这一行很重要)
android:configChanges="orientation|keyboardHidden|keyboard|navigation"

锁屏状态下点亮屏幕,并弹出闹钟提示信息