bear_9912
bear_9912
  • 发布:2016-04-14 11:37
  • 更新:2022-04-29 15:36
  • 阅读:6444

闹铃提醒

分类:Native.js

项目需要,做了一个闹铃提醒的例子。


<!DOCTYPE html>  
<html>  

    <head>  
        <meta charset="utf-8">  
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
        <title>TTimer</title>  
        <script src="js/mui.min.js"></script>  
        <link href="http://ask.dcloud.net.cn/css/mui.min.css" rel="stylesheet" />  
    </head>  

    <body style="padding: 40px;">  
        <button id="alarm"> 插入闹铃</button>  
    </body>  
    <script type="text/javascript" charset="utf-8">  
        mui.init();  
        mui.plusReady(function() {  
            //  
            mui("body").on('tap', 'button', function(event) {  
                var id = this.getAttribute('id');  
                if (id == "alarm") {  
                    addAlarm();  
                }  
            });  
            var main = plus.android.runtimeMainActivity();  
            // 广播接收  
            var ALARM_RECEIVER = "alarm_receiver";  
            var receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {  
                onReceive: function(context, intent) { //实现onReceiver回调函数  
                    plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作  
                    //console.log(intent.getAction()); //获取action  
                    var txt = "接收到消息"   intent.getAction()   ": "   getCurTime();  
                    mui.toast(txt);  
                    pushMsg(txt);  
                    main.unregisterReceiver(receiver); //取消监听  
                }  
            });  
            //  
            function addAlarm() {  
                // --- 注册监听 start ---  
                var IntentFilter = plus.android.importClass('android.content.IntentFilter');  
                var filter = new IntentFilter(ALARM_RECEIVER);  
                main.registerReceiver(receiver, filter);  
                // --- 注册监听 end ---  
                // --- 设置闹铃时间 start ---  
                var Calendar = plus.android.importClass('java.util.Calendar');  
                var calendar = Calendar.getInstance();  
                // 11点4分执行  
                //calendar.set(Calendar.HOUR_OF_DAY, 11);  
                //calendar.set(Calendar.MINUTE, 4);  
                //calendar.set(Calendar.SECOND, 0);  
                // 过5s 执行  
                calendar.setTimeInMillis(Date.parse(new Date()));  
                calendar.add(Calendar.SECOND, 5);  
                // --- 设置闹铃时间 end ---  
                // --- 设置闹铃 ---  
                var Intent = plus.android.importClass('android.content.Intent');  
                var intent = new Intent(ALARM_RECEIVER);  
                //  
                var PendingIntent = plus.android.importClass('android.app.PendingIntent');  
                var sender = PendingIntent.getBroadcast(main, 0, intent, 0);  
                //  
                var AlarmManager = plus.android.importClass("android.app.AlarmManager");  
                var Context = plus.android.importClass("android.content.Context");  
                var alarm = main.getSystemService(Context.ALARM_SERVICE);  
                // 一次    
                alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);  
                // 重复多次  
                //alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);  
                mui.toast("设置闹铃5秒后提醒: "   getCurTime());  
            };  
            //  
            function pushMsg(content) {  
                var options = {  
                    "title": "闹铃提醒",  
                };  
                plus.push.createMessage(content, "alarm", options);  
            };  
            //  
            function getCurTime() {  
                var d = new Date();  
                return d.getFullYear()   "-"   (d.getMonth()   1)   "-"   d.getDate()   " "   d.getHours()   ":"   d.getMinutes()   ":"   d.getSeconds();  
            };  
        });  
    </script>  

</html>  
6 关注 分享
素颜 BoredApe amourz 小云菜 菜鸡 5***@qq.com

要回复文章请先登录注册

redzippo

redzippo

您好,新手一枚,正好也做到类似您的闹铃应用,发现重复提醒的时候,虽然设置了
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
为5 * 1000 但还是很有规律的1分钟一唤醒,请问您知道原因不?
另外我按照您的写法,页面总是报错 alarm未定义,只能改为如下写法:
var AlarmsManager = plus.android.importClass("android.app.AlarmManager");
var alarmsManager = main.getSystemService(Context.ALARM_SERVICE);
plus.android.invoke(alarmsManager, "setRepeating", alarmsManager.RTC_WAKEUP, new Date().getTime(), 16, sender);
不知是不是native.js更新导致的。
谢谢
2016-07-13 17:36