標籤:android style io 使用 ar sp cti 代碼 on
鎖屏狀態下點亮螢幕,並彈出鬧鐘提示資訊,可以在鎖定畫面上取消鬧鐘;使用廣播接收鬧鐘定時:
下面是例子裡的核心代碼如下
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"
鎖屏狀態下點亮螢幕,並彈出鬧鐘提示資訊