`

简单抓取服务器端推送消息的思路

 
阅读更多
这个推送消息的模型就是从Service启动一个线程,定期获取服务器端消息然后显示出来:

MessageService.java文件:
package com.text.ac;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class MessageService extends Service {

	private Notification mNotification = null;

	private NotificationManager mNotifyManager = null;

	private Intent mIntent = null;
	private PendingIntent mPendingIntent = null;
	/** 获取消息线程 */
	private MessageThread mMsgThread = null;
	private int messageNotificationID = 1000;

	public IBinder onBind(Intent intent) {
		return null;
	}

	/** Called by the system when the service is first created. */
	@Override
	public void onCreate() {
		super.onCreate();

		mNotification = new Notification();
		/**
		 * The resource id of a drawable to use as the icon in the status bar.
		 * This is required; notifications with an invalid icon resource will
		 * not be shown.
		 */
		mNotification.icon = R.drawable.icon;
		mNotification.tickerText = "新消息";
		mNotification.defaults = Notification.DEFAULT_SOUND;
		mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		/** 点击跳转的activity. */
		mIntent = new Intent(this, ExTextActivity.class);
		mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

		mMsgThread = new MessageThread();
		mMsgThread.isRunning = true;
		mMsgThread.start();
	}

	class MessageThread extends Thread {

		public boolean isRunning = true;

		public void run() {
			while (isRunning) {
				try {
					Thread.sleep(5000);
					/** 获取服务器消息 . */
					String mServerMsg = getServerMessage();

					if (mServerMsg != null && !"".equals(mServerMsg)) {
						/** 更新通知栏. */
						mNotification
								.setLatestEventInfo(MessageService.this, "新消息",
										"您中奖了,1个亿!" + mServerMsg,
										mPendingIntent);
						mNotifyManager.notify(messageNotificationID,
								mNotification);
						/** 每次通知完,通知ID递增一下,避免消息覆盖掉. */
						messageNotificationID++;
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		System.exit(0);
		/** 使用System.exit(0),这样进程退出的更干净. */
		mMsgThread.isRunning = false;
		super.onDestroy();
	}

	/**
	 * 这里以此方法为服务器Demo,仅作示例
	 * 
	 * @return 返回服务器要推送的消息,否则如果为空的话,不推送
	 */
	public String getServerMessage() {
		return "YES!";
	}
}
分享到:
评论

相关推荐

    天天8点读全国

    7. **推送通知**:为了让用户及时获取新内容,应用可能集成了推送通知服务,如Firebase Cloud Messaging(FCM)或Apple Push Notification service(APNs)。 8. **云服务**:为了存储和处理大量数据,应用可能会...

    qq协议文档

    8. **推送通知**:QQ的实时性要求它能快速响应新消息,这可能涉及到服务器向客户端推送通知的技术,如APNS(Apple Push Notification Service)或自建推送服务。 9. **P2P技术**:为了提高效率,QQ可能会使用P2P...

    留言本模块 留言本模块

    4. 回复通知:当用户收到新的回复时,可以通过邮件、消息推送等方式及时通知。 5. SEO优化:为了让搜索引擎更好地抓取和理解留言内容,可以适当地进行SEO优化,例如添加合适的meta标签,合理使用关键词等。 总结,...

    高性能页面加载技术--BigPipe设计原理及Java简单实现

    - **Server-Sent Events (SSE)** 或 **WebSocket**:用于实现实时推送,服务器向客户端发送数据流,客户端可以实时接收和处理。 - **Ajax**:用于异步请求各个管道的数据,避免阻塞页面加载。 - **浏览器缓存**:...

    基于Socket的Android手机视频实时传输所有源程序

    在这个项目中,Android客户端可能使用了Camera API来捕获视频,再通过Socket将其编码并发送到服务器。 "手机"指的是Android设备,这里的手机不仅是视频的来源,也是控制视频传输的终端。通过用户界面,用户可以启动...

    crowdshopping:社区商务平台,用于分享社交网络的价格和客户情绪分析师

    JavaScript结合机器学习库(如TensorFlow.js)可以在浏览器端进行轻量级的情感分析,无需依赖服务器端的强大计算资源。 在crowdshopping平台上,用户可以通过浏览商品,查看价格走势,阅读其他用户的评价,甚至参与...

Global site tag (gtag.js) - Google Analytics