Android 自带Apps 学习---AlarmClock

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)

Since: API Level 1

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值