设为首页 加入收藏

TOP

Android通过服务实现消息推送
2014-11-24 02:29:30 来源: 作者: 【 】 浏览:0
Tags:Android 通过 服务 实现 消息 推送

android项目中,有时会有这样一种需求:客户每隔一段时间,就像服务器发送一个请求,以获取某些重要的、实时更新的消息。比如天气预报。


如何让应用实现在后台一直处于运行状态,并且每个一段时间就向服务器发一个请求?android里的四大组件之一:服务,就为我们提供了这种功能。


因此,我们可以尝试在服务里边定义一个线程,只要服务不停止,线程就一直在运行,让它每隔一段时间,就向服务器发送一个请求。


这里只贴出核心代码


Android通过服务实现消息推送源码下载


具体下载目录在 /2013年资料/11月/25日/Android通过服务实现消息推送


我们来看一下核心代码


1.在Activity中,我们可以通过startService()来启动服务


public void open(View view) {
Intent intent = new Intent(this, PushSmsService.class);
// 启动服务
startService(intent);
}


2.这里我们需要自定义一个服务类,去继承android的Service类


/**
*
* 短信推送服务类,在后台长期运行,每个一段时间就向服务器发送一次请求
*
* @author jerry
*
*/
public class PushSmsService extends Service {
private MyThread myThread;
private NotificationManager manager;
private Notification notification;
private PendingIntent pi;
private AsyncHttpClient client;
private boolean flag = true;


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate() {
System.out.println("oncreate()");
this.client = new AsyncHttpClient();
this.myThread = new MyThread();
this.myThread.start();
super.onCreate();
}


@Override
public void onDestroy() {
this.flag = false;
super.onDestroy();
}


private void notification(String content, String number, String date) {
// 获取系统的通知管理器
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_menu_compose, content,
System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除


Intent intent = new Intent(getApplicationContext(),
ContentActivity.class);
intent.putExtra("content", content);
intent.putExtra("number", number);
intent.putExtra("date", date);


pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

notification.setLatestEventInfo(getApplicationContext(), number
+ "发来短信", content, pi);


// 将消息推送到状态栏
manager.notify(0, notification);


}


private class MyThread extends Thread {
@Override
public void run() {
String url = "http://110.65.99.66:8080/jerry/PushSmsServlet";
while (flag) {
System.out.println("发送请求");
try {
// 每个10秒向服务器发送一次请求
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}


// 采用get方式向服务器发送请求
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
try {
JSONObject result = new JSONObject(new String(
responseBody, "utf-8"));
int state = result.getInt("state");
// 假设偶数为未读消息
if (state % 2 == 0) {
String content = result.getString("content");
String date = result.getString("date");
String number = result.getString("number");
notification(content, number, date);
}
} catch (Exception e) {
e.printStackTrace();
}


}


@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "数据请求失败", 0)
.show();
}
});


}
}
}


}


相关阅读


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android加载大图片到内存 下一篇Android从图库(Gallery)选择一张..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: