用HTML+JS实现Android闹钟功能 附Alarm代码分享

移动开发 Android
刚接触Android,研究了Rexsee的源码,分享这段Alarm的代码。先是开发手册中最终功能的实现函数,再是Alarm接口的源码。

参数:

argu:型如“key1=value1;key2=value2;......”的参数表。首先,该参数表支持rexseeNotification.show()函数的所有参数,用于显示通知(调用rexseeNotification.show()),请参见rexseeNotification。另外,该参数表增加了以下参数:

forcerepeat:true或false。当该闹钟是由推送信息而非页面设定时,如果id和之前的推送信息的id重复,由该参数决定是否强制重新执行,默认为false,即不会重复执行任何id重复的推送信息。

command:闹钟响时要执行的命令,目前支持的命令包括:

notification:发送通知,默认值。

startApplication:启动程序。

cleanApplicationData:清楚本程序的业务数据(私有内存中的所有数据)。

notificationimmediately:true或false,无论命令是否notification,该参数都允许系统在设置闹钟的***时间先发送一个通知,然后在指定的时间延迟后再执行命令,默认为false。

notificationafterexec:true或false,无论命令是否notification,该参数都允许系统在执行完命令后发送一个通知,默认为false。

alermname:闹钟的名称,默认为"defaultAlerm"。

alermfirsttime:时间戳,***次闹钟响(即执行命令)的时间,如果设为0或其他小于当前时间的时间戳,命令将立即执行,默认为立即执行。

alermrepeatinterval:毫秒数,***次闹钟响之后,间隔该时间后重复执行命令,如果小于零,将不会重复执行。

startApplicationUrl:如果命令为startApplication,程序启动后访问的URL地址。

示例:

  1. exseeAlarm.set('command=startApplication;startApplicationUrl=http://www.rexsee.com/rexsee/alarmClock.html;alermName=test;alermfirsttime='+(rexseeAlarm.getCurrentTime()+5000)+';title=闹钟测试;message=闹钟测试内容;url=http://www.rexsee.com/rexsee/alarmClock.html');  
  2. rexseeDialog.toast('设置完毕!');  

Rexsee的Android Alarm源码如下:

  1. /*  
  2. * Copyright (C) 2011 The Rexsee Open Source Project  
  3. *  
  4. * Licensed under the Rexsee License, Version 1.0 (the "License");  
  5. * you may not use this file except in compliance with the License.  
  6. * You may obtain a copy of the License at  
  7. *  
  8. *      http://www.rexsee.com/CN/legal/license.html  
  9. *  
  10. * Unless required by applicable law or agreed to in writing, software  
  11. * distributed under the License is distributed on an "AS IS" BASIS,  
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. * See the License for the specific language governing permissions and  
  14. * limitations under the License.  
  15. */  
  16.  
  17. package rexsee.core.alarm;   
  18.  
  19. import rexsee.core.browser.JavascriptInterface;   
  20. import rexsee.core.browser.RexseeBrowser;   
  21. import rexsee.core.device.NotificationArgumentsSheet;   
  22. import rexsee.core.device.RexseeNotification;   
  23. import rexsee.core.receiver._Receiver;   
  24. import android.app.AlarmManager;   
  25. import android.app.PendingIntent;   
  26. import android.content.Context;   
  27. import android.content.Intent;   
  28. import android.database.Cursor;   
  29. import android.database.sqlite.SQLiteDatabase;   
  30.  
  31. public class RexseeAlarm implements JavascriptInterface {   
  32.  
  33.        private static final String INTERFACE_NAME = "Alarm";   
  34.        @Override   
  35.        public String getInterfaceName() {   
  36.                return mBrowser.application.resources.prefix + INTERFACE_NAME;   
  37.        }   
  38.        @Override   
  39.        public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {   
  40.                return this;   
  41.        }   
  42.        @Override   
  43.        public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {   
  44.                return new RexseeAlarm(childBrowser);   
  45.        }   
  46.  
  47.        public static final String ALARM_ACTION = "action.alarm.id_";   
  48.        public static final String ALARM_EXTRA_ARGU = "argu";   
  49.  
  50.        public static final String DATABASE_ALARM = "alarm.db";   
  51.        public static final String TABLE_ALARM = "alarm";   
  52.  
  53.        private final Context mContext;   
  54.        private final RexseeBrowser mBrowser;   
  55.  
  56.        public RexseeAlarm(RexseeBrowser browser) {   
  57.                mBrowser = browser;   
  58.                mContext = browser.getContext();   
  59.        }   
  60.        public RexseeAlarm(Context context) {   
  61.                mBrowser = null;   
  62.                mContext = context;   
  63.        }   
  64.  
  65.        private static void _setAlarm(Context context, AlarmManager mgr, String body, boolean save) {   
  66.                NotificationArgumentsSheet argu = (new NotificationArgumentsSheet()).parseArguments(body);   
  67.                if (argu.notificationimmediately) {   
  68.                        (new RexseeNotification(context)).show(argu);   
  69.                }   
  70.                if (argu.getAlermFirstTime() > System.currentTimeMillis()) {   
  71.                        Intent intent = new Intent(context, _Receiver.class);   
  72.                        intent.setAction(ALARM_ACTION + argu.alermname);   
  73.                        intent.putExtra(ALARM_EXTRA_ARGU, body);   
  74.                        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);   
  75.                        mgr.cancel(pendingIntent);   
  76.                        long interval = argu.getAlermRepeatInterval();   
  77.                        if (interval > 0) {   
  78.                                mgr.setRepeating(AlarmManager.RTC_WAKEUP, argu.getAlermFirstTime(), interval, pendingIntent);   
  79.                        } else {   
  80.                                mgr.set(AlarmManager.RTC_WAKEUP, argu.getAlermFirstTime(), pendingIntent);   
  81.                        }   
  82.                        if (save) {   
  83.                                SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);   
  84.                                try {   
  85.                                        db.execSQL("CREATE TABLE if not exists " + TABLE_ALARM + " (name TEXT, argu TEXT, Primary key(name));");   
  86.                                        db.execSQL("DELETE FROM " + TABLE_ALARM + " WHERE name='" + argu.alermname + "';");   
  87.                                        db.execSQL("INSERT INTO " + TABLE_ALARM + " VALUES ('" + argu.alermname + "', '" + body + "');");   
  88.                                } catch (Exception e) {   
  89.                                }   
  90.                                db.close();   
  91.                        }   
  92.                } else {   
  93.                        exec(context, body);   
  94.                }   
  95.        }   
  96.        private static void _deleteAlarm(Context context, String name) {   
  97.                SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);   
  98.                try {   
  99.                        db.execSQL("DELETE FROM " + TABLE_ALARM + " WHERE name='" + name + "';");   
  100.                } catch (Exception e) {   
  101.                }   
  102.                db.close();   
  103.        }   
  104.  
  105.        public static void exec(Context context, String body) {   
  106.                NotificationArgumentsSheet argu = (new NotificationArgumentsSheet()).parseArguments(body);   
  107.                if (argu.getAlermRepeatInterval() <= 0) {   
  108.                        _deleteAlarm(context, argu.alermname);   
  109.                }   
  110.                (new RexseeRemoteCommand(context, body)).exec();   
  111.        }   
  112.        public static void updateAlarm(Context context) {   
  113.                SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);   
  114.                AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);   
  115.                try {   
  116.                        db.execSQL("CREATE TABLE if not exists " + TABLE_ALARM + " (name TEXT, argu TEXT, Primary key(name));");   
  117.                        Cursor cursor = db.rawQuery("SELECT * from " + TABLE_ALARM + ";", null);   
  118.                        if (cursor != null && cursor.getCount() != 0) {   
  119.                                for (int i = 0; i < cursor.getCount(); i++) {   
  120.                                        cursor.moveToPosition(i);   
  121.                                        _setAlarm(context, mgr, cursor.getString(1), false);   
  122.                                }   
  123.                        }   
  124.                        cursor.close();   
  125.                } catch (Exception e) {   
  126.                }   
  127.                db.close();   
  128.        }   
  129.  
  130.        //JavaScript Interface   
  131.        public void set(String body) {   
  132.                _setAlarm(mContext, (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE), body, true);   
  133.        }   
  134.        public String get() {   
  135.                SQLiteDatabase db = mContext.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);   
  136.                String rtn = "";   
  137.                try {   
  138.                        Cursor cursor = db.rawQuery("SELECT * from " + TABLE_ALARM + ";", null);   
  139.                        if (cursor != null && cursor.getCount() != 0) {   
  140.                                for (int i = 0; i < cursor.getCount(); i++) {   
  141.                                        cursor.moveToPosition(i);   
  142.                                        if (i > 0) rtn += ",";   
  143.                                        rtn += "{";   
  144.                                        rtn += "\"name\":\"" + cursor.getString(0) + "\"";   
  145.                                        rtn += ",\"argu\":\"" + cursor.getString(1) + "\"";   
  146.                                        rtn += "}";   
  147.                                }   
  148.                        }   
  149.                        cursor.close();   
  150.                } catch (Exception e) {   
  151.                        if (mBrowser != null) mBrowser.exception(getInterfaceName(), e);   
  152.                }   
  153.                db.close();   
  154.                return "[" + rtn + "]";   
  155.        }   
  156.        public void cancel(String name) {   
  157.                Intent intent = new Intent(mContext, _Receiver.class);   
  158.                intent.setAction(ALARM_ACTION + name);   
  159.                PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);   
  160.                AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);   
  161.                mgr.cancel(pendingIntent);   
  162.                _deleteAlarm(mContext, name);   
  163.        }   
  164.  
  165.        public long getCurrentTime() {   
  166.                return System.currentTimeMillis();   
  167.        }   
  168.        public long getMillisPerHour() {   
  169.                return 3600 * 1000;   
  170.        }   
  171.        public long getMillisPerDay() {   
  172.                return 3600 * 1000 * 24;   
  173.        }   
  174.  
  175. }  

 

责任编辑:佚名 来源: Iteye
相关推荐

2016-08-11 08:24:39

AndroidIntentShareTestDe

2010-01-27 18:06:03

Android短信发送

2009-06-16 10:44:50

JS代码折叠Visual Stud

2012-12-28 14:32:34

Android开发Handler异步处理

2012-06-27 10:03:39

PHP

2011-06-01 13:22:25

Android Alarm

2010-09-17 10:26:01

iPhone

2020-12-09 11:32:10

CSS前端代码

2020-03-09 14:08:25

Python目标检测视觉识别

2021-07-22 10:25:07

JS验证码前端

2010-02-05 18:09:28

C++ Doxygen

2010-01-26 13:55:57

Android分享功能

2023-08-26 07:09:36

2018-02-08 16:45:22

前端JS粘贴板

2024-03-21 08:50:33

HTMLURL预览应用软件

2013-07-03 16:49:17

AndroidWebView

2010-01-28 15:26:33

Android调用平台

2012-12-31 13:36:28

Android开发Alarmmanage

2012-05-10 15:41:46

HTML5

2009-07-03 09:44:39

实现RSS功能JSP技术
点赞
收藏

51CTO技术栈公众号