`
rexsee
  • 浏览: 20005 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Rexsee API介绍:Android定时任务Alarm,附基本的闹钟功能示例

阅读更多

 

利用Rexsee扩展的Alarm可以快速实现关于任务定时的相关功能,基本的闹钟事件如下

 

【函数】 void set(String argu)

【说明】 设置闹钟。

【返回】

【参数】  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地址。

【示例】

 

Html代码 
  1. rexseeAlarm.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('设置完毕!');  

 

【函数】 JsonObjectArray get()

【说明】 读取所有的闹钟信息。

【返回】 JSON对象数组,使用eval('('+json+')')转换为JavaScript对象数组。

【参数】

【示例】

 

Html代码 
  1. alert(rexseeAlarm.get());  

 

【函数】 void cancel(String name)

【说明】 取消闹钟。

【返回】

【参数】 name:闹钟名称。

【示例】

 

Html代码 
  1. rexseeAlarm.cancel('test');  
  2. rexseeDialog.toast('取消完毕!');  

 

rexseeAlarm.java源码如下:

 

/* 
* 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.notificationimmediately) {  
                       (new RexseeNotification(context)).show(argu);  
               }  
               if (argu.getAlermFirstTime() > System.currentTimeMillis()) {  
                       Intent intent = new Intent(context, _Receiver.class);  
                       intent.setAction(ALARM_ACTION + argu.alermname);  
                       intent.putExtra(ALARM_EXTRA_ARGU, body);  
                       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
                       mgr.cancel(pendingIntent);  
                       long interval = argu.getAlermRepeatInterval();  
                       if (interval > 0) {  
                               mgr.setRepeating(AlarmManager.RTC_WAKEUP, argu.getAlermFirstTime(), interval, pendingIntent);  
                       } else {  
                               mgr.set(AlarmManager.RTC_WAKEUP, argu.getAlermFirstTime(), pendingIntent);  
                       }  
                       if (save) {  
                               SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);  
                               try {  
                                       db.execSQL("CREATE TABLE if not exists " + TABLE_ALARM + " (name TEXT, argu TEXT, Primary key(name));");  
                                       db.execSQL("DELETE FROM " + TABLE_ALARM + " WHERE name='" + argu.alermname + "';");  
                                       db.execSQL("INSERT INTO " + TABLE_ALARM + " VALUES ('" + argu.alermname + "', '" + body + "');");  
                               } catch (Exception e) {  
                               }  
                               db.close();  
                       }  
               } else {  
                       exec(context, body);  
               }  
       }  
       private static void _deleteAlarm(Context context, String name) {  
               SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);  
               try {  
                       db.execSQL("DELETE FROM " + TABLE_ALARM + " WHERE name='" + name + "';");  
               } catch (Exception e) {  
               }  
               db.close();  
       }  
 
       public static void exec(Context context, String body) {  
               NotificationArgumentsSheet argu = (new NotificationArgumentsSheet()).parseArguments(body);  
               if (argu.getAlermRepeatInterval() <= 0) {  
                       _deleteAlarm(context, argu.alermname);  
               }  
               (new RexseeRemoteCommand(context, body)).exec();  
       }  
       public static void updateAlarm(Context context) {  
               SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);  
               AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
               try {  
                       db.execSQL("CREATE TABLE if not exists " + TABLE_ALARM + " (name TEXT, argu TEXT, Primary key(name));");  
                       Cursor cursor = db.rawQuery("SELECT * from " + TABLE_ALARM + ";", null);  
                       if (cursor != null && cursor.getCount() != 0) {  
                               for (int i = 0; i < cursor.getCount(); i++) {  
                                       cursor.moveToPosition(i);  
                                       _setAlarm(context, mgr, cursor.getString(1), false);  
                               }  
                       }  
                       cursor.close();  
               } catch (Exception e) {  
               }  
               db.close();  
       }  
 
       //JavaScript Interface  
       public void set(String body) {  
               _setAlarm(mContext, (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE), body, true);  
       }  
       public String get() {  
               SQLiteDatabase db = mContext.openOrCreateDatabase(DATABASE_ALARM, Context.MODE_PRIVATE, null);  
               String rtn = "";  
               try {  
                       Cursor cursor = db.rawQuery("SELECT * from " + TABLE_ALARM + ";", null);  
                       if (cursor != null && cursor.getCount() != 0) {  
                               for (int i = 0; i < cursor.getCount(); i++) {  
                                       cursor.moveToPosition(i);  
                                       if (i > 0) rtn += ",";  
                                       rtn += "{";  
                                       rtn += "\"name\":\"" + cursor.getString(0) + "\"";  
                                       rtn += ",\"argu\":\"" + cursor.getString(1) + "\"";  
                                       rtn += "}";  
                               }  
                       }  
                       cursor.close();  
               } catch (Exception e) {  
                       if (mBrowser != null) mBrowser.exception(getInterfaceName(), e);  
               }  
               db.close();  
               return "[" + rtn + "]";  
       }  
       public void cancel(String name) {  
               Intent intent = new Intent(mContext, _Receiver.class);  
               intent.setAction(ALARM_ACTION + name);  
               PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
               AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);  
               mgr.cancel(pendingIntent);  
               _deleteAlarm(mContext, name);  
       }  
 
       public long getCurrentTime() {  
               return System.currentTimeMillis();  
       }  
       public long getMillisPerHour() {  
               return 3600 * 1000;  
       }  
       public long getMillisPerDay() {  
               return 3600 * 1000 * 24;  
       }  
 
}
分享到:
评论

相关推荐

    rexsee jar

    超过2000个JavaScript扩展API,功能强大。 支持第三方JavaScript开发框架。 B/C/S混合架构,支持应用程序本地化,摆脱网络依赖。 全面支持Android原生UI布局,通过JS或XML生成原生布局。 全面支持Android硬件,包括...

    使用Rexsee EMS开发Android手机应用:为什么及如何开始

    了解使用Rexsee EMS开发Android手机应用的好资料

    Rexsee 源代码

    超过2000个JavaScript扩展API,功能强大。 支持第三方JavaScript开发框架。 B/C/S混合架构,支持应用程序本地化,摆脱网络依赖。 全面支持Android原生UI布局,通过JS或XML生成原生布局。 全面支持Android硬件,包括...

    rexsee -src.zip

    超过2000个JavaScript扩展API,功能强大。 支持第三方JavaScript开发框架。 B/C/S混合架构,支持应用程序本地化,摆脱网络依赖。 全面支持Android原生UI布局,通过JS或XML生成原生布局。 全面支持Android硬件,包括...

    Rexsee开发手册的zip文件

    Rexsee是基于Android的HTML5开发平台,帮助开发者使用HTML5+JavaScript开发Android应用。

    rexsee 最新软件源代码

    Rexsee是基于Android的HTML5开发平台,帮助开发者使用HTML5+JavaScript开发Android应用

    Android移动中间件Rexsee开发手册

    Android移动中间件Rexsee开发手册,利用它可以快速开发Android应用程序,只需要你掌握HTML+CSS+JavaScript,而需要掌握java和Android SDK。让你快速得进入到移动开发的大门。

    rexsee手机本地版开发手册

    rexsee手机本地版开发手册

    Rexsee源代码

    Rexsee源代码,Rexsee系统的源代码。从他的网站上一点一点搞下来的。哈哈。。你懂的。

    rexsee文档和api使用groovy爬下来分享给大家

    NULL 博文链接:https://key232323.iteye.com/blog/1779445

    rexsee非官方菜鸟安装文档

    rexsee目前取消了在线生成功能,只提供了简单的安装指引,但这个指引对于安卓菜鸟来说过于简单,而且其中有几处重大缺漏。本人对官方文档进行了补充说明,并介绍了自己遇到的经验教训,希望对擅长webapp开发,而又不...

    android使用webwiew载入页面使用示例(Hybrid App开发)

    目前大家所知道的基于中间件的移动开发框架都是采用的 Hybrid 开发模式,例如国外的 PhoneGap、Titanium、Sencha,还有国内的 AppCan、Rexsee 等等。Hybrid App 开发模式正在被越来越多的公司和开发者所认同,相信...

    开源Rexsee模糊原生应用与Web应用界线

    Web应用,Rexsee,开源,Web开发,JS,Android,Web技术,应用趋势,应用范围,移动终端,,开源Rexsee模糊原生应用与W,开源Rexsee模糊原生应用与Web应用界线

    ESM企业销售管理系统(B/S+mobile) v3.0

    摘要:PHP源码,管理系统,ESM,销售管理系统 ESM企业... 中间件层包括函数库rexsee,由java开发,android操作系统、中间件、用户界面和应用软件组成。    服务器端新增功能模块:  1.客户拜访 2.公告文档 3.产品管理 4

Global site tag (gtag.js) - Google Analytics