1: 启动App在前台,即使已经锁屏
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);
//去除Activity标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//布局解析器
LayoutInflater mFactory = LayoutInflater.from(this);
mFactory.inflate(AlarmClock.CLOCKS[position], null);
SharedPreferences 共享存储
SharedPreferences mPrefs = getSharedPreferences(AlarmClock.PREFERENCES, 0);
int face = mPrefs.getInt(AlarmClock.PREF_CLOCK_FACE, 0);
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt(AlarmClock.PREF_CLOCK_FACE, position);
ed.commit();
2:对Provider 数据改动注册监听
private class FormatChangeObserver extends ContentObserver {
public FormatChangeObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
setDateFormat();
updateTime();
}
}
mFormatChangeObserver = new FormatChangeObserver();
mContext.getContentResolver().registerContentObserver(
Settings.System.CONTENT_URI, true, mFormatChangeObserver);
Settings.System.CONTENT_URI 可用任意Provider的URi进行替换,
在provider中可通过getContentResolver().notifyChange(uri,..)提示更改,call back ContentObserver
public void notifyChange (Uri uri, ContentObserver observer)
Notify registered observers that a row was updated. To register, call registerContentObserver()
. By default, CursorAdapter objects will get this notification.
Parameters
observer | The observer that originated the change, may be null |
---|
3:键盘锁管理器
KeyguardManager km =
(KeyguardManager) context.getSystemService(
Context.KEYGUARD_SERVICE);
//If keyguard screen is showing or in restricted key input mode
km.inKeyguardRestrictedInputMode()
//开启键盘锁
km.newKeyguardLock(String tag)
//关闭,带回调接口
km.exitKeyguardSecurely(OnKeyguardExitResult arg0)
4: PowerManager.WakeLock
在Android中,申请WakeLock可以让你的进程持续执行即使手机进入睡眠模式,
比较实用的是比如:
后台有网络功能,可以保证操作持续进行
PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, Log.LOGTAG);
sCpuWakeLock.acquire();
释放
if (sCpuWakeLock != null) {
sCpuWakeLock.release();
sCpuWakeLock = null;
}
5: Alarm Manager
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, Intent);
am.setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
am.setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)