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地址。
【示例】
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');
rexseeDialog.toast('設置完畢!');

【函數】 JsonObjectArray get()
【說明】 讀取全部的鬧鐘信息。
【返回】 JSON對象數組,使用eval('('+json+')')轉換爲JavaScript對象數組。
【參數】
【示例】
alert(rexseeAlarm.get());

【函數】 void cancel(String name)
【說明】 取消鬧鐘。
【返回】
【參數】 name:鬧鐘名稱。
【示例】
rexseeAlarm.cancel('test');
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的源碼和函數事件作了整理,相關的demo或源碼解析能夠在Rexsee社區瞭解,目前Rexsee已提供了近2000個擴展,覆蓋Android原生功能超過90%,且所有開放: http://www.rexsee.com/
相關文章
相關標籤/搜索