Android闹钟遇到的那些坑

做过闹钟的话你就知道这中间有多少坑了。

第一次做闹钟程序是在2012年,那时候android最新版本是2.2,2.3发布在即,做了一个整点提醒的小工具,记得很清楚,主要的问题是锁屏之后闹钟不能准时被唤醒,总会晚那么几秒钟,后来没办法把闹钟提前设置几秒钟。不过那时候环境还好,没有遇到攻克不了的问题,重启也可以唤起闹钟的。

但是随着android版本的进化,开发者节操的丢失,问题就越来越难做了。闹钟明明设置了却不能到来;不再设置的时间到来,晚了好久才到;重启之后闹钟就没了等等。当然,还有好多好多,总之很多东西不按照自己期待的来。

项目上线,踩过之后挑一些做总结。

由于时间关系,项目比较赶,所以没有去考虑闹钟无法及时触发的问题,假设了一个最理想的环境。(更多的信息可以参考下面的文章)

闹钟的创建

这里涉及到不同版本设置闹钟的方法,下面的参考文章中已经提到:

    private void startAlarm(Calendar calendar, PendingIntent pendingIntent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // mManager.setWindow(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000* 5, mFirstPIntent);
            mManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            // mManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mSecondPIntent);
        } else {
            mManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }

闹钟的取消

有的时候可能需要去取消一个闹钟。我们可以通过两种方式来取消:

  • 通过 PendingIntent 来取消

    PendingIntent.cancel();

    /**
     * Cancel a currently active PendingIntent.  Only the original application
     * owning a PendingIntent can cancel it.
     */
    public void cancel() {
        try {
            ActivityManagerNative.getDefault().cancelIntentSender(mTarget);
        } catch (RemoteException e) {
        }
    }

    这种方式只有能拿到 PendingIntent 才可以。

  • 通过 AlarmManager 来取消

    AlarmManager.cancel(PendingIntent);

    /**
     * Remove any alarms with a matching {@link Intent}.
     * Any alarm, of any type, whose Intent matches this one (as defined by
     * {@link Intent#filterEquals}), will be canceled.
     *
     * @param operation IntentSender which matches a previously added
     * IntentSender.
     *
     * @see #set
     */
    public void cancel(PendingIntent operation) {
        try {
            mService.remove(operation);
        } catch (RemoteException ex) {
        }
    }

闹钟的查看

又个adb命令可以用来查看当前系统中存在的闹钟:

adb shell dumpsys alarm

这里写图片描述

不是在所有的设备上都好使,比如在我的魅族设备上可以正常使用,而在另一台 OPPO R7C上就不可以。

参考:

  1. http://www.jianshu.com/p/1f919c6eeff6
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值