基于Rexsee实现Android平台的推送与监听,附原生代码
在Android实现信息推送有两种常用的方式种,短信或HTTP:1. 短信方式实时性较好,但需要使用者有短信平台发送短信;
2. HTTP方式有一定延时,但使用更方便。
除此之外,还有一种伪推送:浏览器推送。
其实推送的原理非常简单,内容就是一堆参数字符串。如下所示:
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
无论是短信还是HTTP推送, 内容都是一样的,只是推送的形式不一样而已。当然,单方面的推送肯定是没有效果的,接收方还需要进行推送监听的设置,如果是HTTP推送,那么需要设置监听推送的页面;短信的话则是设置推送号码;但是短信监听的话会使得短信变成命令从而存到数据库内,而不会存入收件箱内了。
普通的推送如同提醒一般,会在通知栏上出现图标和提示文字,点击则根据设置的参数作出需要的动作,如打开某个客户端等。但推送的作用远不止如此,通过此类命令,甚至可以对手机进行远程控制。
上面的那一段代码就是一个“在5秒钟后打开闹钟测试页面”的远程操控DEMO。全部的示例代码如下,可以看到基于Rexsee我们完全可以使用HTML+JS实现,正如社区的介绍一下,有兴趣的可以直接去社区查看原生代码:http://www.rexsee.com/
“5秒钟后打开闹钟”Demo分享:
一共分三个页面,第一,面向用户的,让使用者设置监听:
<html>
<head>
<title>test</title>
<script>
function a(){
rexseePushHttpListener.add('http://www.fuwu800.com/yezhenqing/push.html','testUser','testPassword');
rexseePushHttpListener.setDurationAndTimeout(1,10);
rexseePushHttpListener.refresh();
}
</script>
</head>
<body>
<input type="button" value="读取" onclick="a()">
<input type="button" value="清除" onclick="javascript:rexseePushHttpListener.remove('http://www.fuwu800.com/yezhenqing/push.html');">
<a href="rexsee:private_file">asd</a>
</body>
</html>
第二个,被监听的页面,id是用系统用来判断是否已经接收到过命令,若不写则每次自动生成,即时根据监听间隔时间不停执行。
id=123123;command=startApplication;startApplicationUrl=http://www.rexsee.com/rexsee/alarmClock.html;alermName=test;alermfirsttime=0;notificationimmediately=true;title=闹钟测试;message=闹钟测试内容;url=http://www.rexsee.com/rexsee/alarmClock.html
第三个,根据被监听页面的参数去执行的页面,该页面会自动解屏解锁然后运行。
<HTML>
<HEAD>
<TITLE>Rexsee闹钟示例</TITLE>
<META http-equiv=Content-Type content=text/html charset=GB2312>
<META http-equiv=Expires content=0>
<META http-equiv=Cache-Control content=no-cache>
<META http-equiv=Pragma content=no-cache>
<script>
rexseeWakeLock.acquire(false);
rexseeKeyguard.disable();
rexseeVibrate.vibrate(1000);
window.onException = function(className, message){
alert("Class: "+className+"\n\nError: "+message);
}
</SCRIPT>
<HEAD>
<BODY>
<table width=100% height=100%><tr><td align=center valign=middle style="font-size:48px;font-weight:bold;">
时间到了!
<br><button style="font-size:36px;font-weight:bold;" onclick="rexseeKeyguard.reEnable();rexseeWakeLock.release();rexseeApplication.quit();">结束</button>
</td></tr></table>
</BODY>
</HTML>
最后附上Rexsee实现的Android推送功能源码,供参考。。考虑到推送是一个非常强大的功能,大家别用来做坏事哈~~
/*
* 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.service;
import rexsee.core.alarm.RexseeAlarm;
import rexsee.core.device.NotificationArgumentsSheet;
import rexsee.core.utilities.Escape;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class RexseePush {
public static final String DATABASE_PUSH = "push.db";
public static final String TABLE_PUSH = "push";
public static final String TABLE_PUSH_SMS = "push_sms";
public static final String TABLE_PUSH_HTTP = "push_http";
public static final String TABLE_PUSH_HTTP_ARGUMENTS = "push_http_arguments";
public static final String PUSH_TYPE_HTTP = "http";
public static final String PUSH_TYPE_SMS = "sms";
public static final String PUSH_TYPE_BROWSER = "browser";
public static final int PUSH_STATUS_TODO = 0;
public static final int PUSH_STATUS_DONE = 1;
public static void initDatabase(Context context) {
SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_PUSH, Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE if not exists " + TABLE_PUSH + " (id int, date long, type Text, done int, address TEXT, arguments TEXT, Primary key(id));");
db.execSQL("CREATE TABLE if not exists " + TABLE_PUSH_SMS + " (phoneNumber TEXT);");
db.execSQL("CREATE TABLE if not exists " + TABLE_PUSH_HTTP + " (url TEXT,userId TEXT, userPassword TEXT, encoding Text);");
db.execSQL("CREATE TABLE if not exists " + TABLE_PUSH_HTTP_ARGUMENTS + " (duration int,timeout int);");
db.close();
}
public static void add(Context context, String type, long when, String address, String body) {
if (body == null) return;
NotificationArgumentsSheet argu = (new NotificationArgumentsSheet()).parseArguments(body);
int id = argu.getId((int) when);
try {
SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_PUSH, Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * from " + TABLE_PUSH + " where id=" + id + ";", null);
if (cursor == null || cursor.getCount() == 0) {
String sql = "INSERT INTO " + TABLE_PUSH + " VALUES (" + id + "," + when + ", '" + type + "', " + PUSH_STATUS_TODO + ", '" + address + "', '" + Escape.escape(body) + "');";
db.execSQL(sql);
(new RexseeAlarm(context)).set(body);
} else {
if (argu.forcerepeat) {
String sql = "UPDATE " + TABLE_PUSH + " SET date=" + when + ",type='" + type + "',done=" + PUSH_STATUS_TODO + ",address='" + address + "',arguments='" + Escape.escape(body) + "' where id=" + id + ";";
db.execSQL(sql);
(new RexseeAlarm(context)).set(body);
}
}
if (cursor != null) cursor.close();
db.close();
} catch (Exception e) {
}
}
} --------------------编程问答-------------------- LZ是有实现么?请与我联系一下,有事请教 --------------------编程问答-------------------- Rexsee到底是啥?一个小团队而已,到处贴源码,就你懂~~
补充:移动开发 , 移动开发其他问题