您的位置:首页 > 移动开发 > Android开发

Android之百度云推送(三)如何启动服务端的定时任务

2016-08-23 17:26 357 查看

Android之百度云推送(三)如何启动服务端的定时任务

apk和服务端都好了,怎么能定时启动服务呢?

查看了好多资料:

spring task定时器的运用

http://blog.csdn.net/xwygn/article/details/8440692

定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器

http://blog.csdn.net/majinggogogo/article/details/49962081

无耐公司spring版本是2.5的不支持

@Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次


升级spring试试,原先的项目报错,不值当的改,改动太大了

一共这么几种定时任务,都不行,怎么办

还是自己玩吧,我选择的是线程,

web项目

第一步写个类启动一个线程,并且希望这个类随web服务器启动而启动

PushJob.java

@Component("PushJob")
public class PushJob  extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
System.out.println("开始 pushjob init .....");
super.init();
PushMsg p = new PushMsg();
Thread t = new Thread(p);
t.start();
System.out.println("完成 pushjob init .....");
}
}


web.xml进行相应的配置随web服务器启动而启动

<servlet>
<servlet-name>PushJob</servlet-name>
<servlet-class>
com.ailk.app.push.servlet.PushJob
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>


第二步修改AndroidPushBatchUniMsg.java为PushMsg.java

听着很奇怪是吧?看我的

public class PushMsg implements Runnable{
@Override
public void run() {
System.out.println("PushMsg running....");
while(true){
doone();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void doone(){
//原先AndroidPushBatchUniMsg.java的main函数修改
// 1. get apiKey and secretKey from developer console
String apiKey = "WnK9iAq465461313GORSyWC";
String secretKey = "MTVkTFfdsaf443Rgb29hF8d";
PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
// 2. build a BaidupushClient object to access released interfaces
BaiduPushClient pushClient = new BaiduPushClient(pair,
BaiduPushConstants.CHANNEL_REST_URL);

// 3. register a YunLogHandler to get detail interacting information
// in this request.
pushClient.setChannelLogHandler(new YunLogHandler() {
@Override
public void onHandle(YunLogEvent event) {
System.out.println(event.getMessage());
}
});

try {
System.out.println("开始了吗");
//从数据库获取消息通知,这个是我自己服务获取的方式,你们改改这就行
PushBean pb = new PushBean();
List<PushInfo> pushList=pb.queryPushList(ac);

System.out.println("有信息吗?"+pushList.size());
if(pushList.size() > 0){

for (PushInfo pushBean : pushList)
{
// 4. specify request arguments
//创建Android通知
JSONObject notification = new JSONObject();
notification.put("title", pushBean.getTitle());
notification.put("description",pushBean.getDescs());
notification.put("notification_builder_id", 0);
notification.put("notification_basic_style", 4);
notification.put("open_type", 3);
JSONObject jsonCustormCont = new JSONObject();
//自定义内容,key-value

//              jsonCustormCont.put("logStaffCode", "zq");
//              jsonCustormCont.put("logStaffId", "982985123456");
//              jsonCustormCont.put("logManagerId", "422493");
//              jsonCustormCont.put("managerTypeId", "49");
//              jsonCustormCont.put("logLatnId", "-1");
//              jsonCustormCont.put("logAreaId", "1");
//              jsonCustormCont.put("H5Url", "http://www.baidu.com");
notification.put("custom_content", jsonCustormCont);
String[] channelIds = { "3898756644656267390" };
PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
.addChannelIds(channelIds)
.addMsgExpires(new Integer(3600))
.addMessageType(1)
.addMessage(notification.toString())
.addDeviceType(3)
.addTopicId(pushBean.getTopicId());// 设置类别主题
// 5. http request
PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request);

// Http请求结果解析打印
System.out.println(String.format("msgId: %s, sendTime: %d",response.getMsgId(), response.getSendTime()));

}

}

} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
try {
throw e;
} catch (PushClientException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
e.printStackTrace();
}
} catch (PushServerException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
try {
throw e;
} catch (PushServerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
System.out.println(String.format(
"requestId: %d, errorCode: %d, errorMessage: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


我想你应该看出来了,其他的都没动,就是你怎么去数据库拿数据了吧。。不懂得可以加群玩android不是梦 261403514咱们商讨一下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息