日期:2014-05-16  浏览次数:20715 次

【源码】实现Android闹钟功能使用HTML+JS,并附带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地址。
示例:
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');
rexseeDialog.toast('设置完毕!');
Rexsee的Android Alarm源码如下:
/* 
* Copyright (C) 2011 The Rexsee Open Source Project 
* 
* Licensed under the Rexsee License, Version 1.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*      http://www.rexsee.com/CN/legal/license.html 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
 
package rexsee.core.alarm;  
 
import rexsee.core.browser.JavascriptInterface;  
import rexsee.core.browser.RexseeBrowser;  
import rexsee.core.device.NotificationArgumentsSheet;  
import rexsee.core.device.RexseeNotification;  
import rexsee.core.receiver._Receiver;  
import android.app.AlarmManager;  
import android.app.PendingIntent;  
import android.content.Context;  
import android.content.Intent;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
 
public class RexseeAlarm implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "Alarm";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {  
               return new RexseeAlarm(childBrowser);  
       }  
 
       public static final String ALARM_ACTION = "action.alarm.id_";  
       public static final String ALARM_EXTRA_ARGU = "argu";  
 
       public static final String DATABASE_ALARM = "alarm.db";  
       public static final String TABLE_ALARM = "alarm";  
 
       private final Context mContext;  
       private final RexseeBrowser mBrowser;  
 
       public RexseeAlarm(RexseeBrowser browser) {  
               mBrowser = browser;  
               mContext = browser.getContext();  
       }  
       public RexseeAlarm(Context context) {  
               mBrowser = null;  
               mContext = context;  
       }  
 
       private static void _setAlarm(Context context, AlarmManager mgr, String body, boolean save) {  
               NotificationArgumentsSheet argu = (new NotificationArgumentsSheet()).parseArguments(body);  
               if (argu.notificationimmedi