从网上查询资料学习Android消息推送机制,效果图如下:

1.首先是布局文件代码 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" /> <Button
android:id="@+id/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="通知" /> <Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="清除" /> </LinearLayout>

2.然后是java主界面代码MainActivity.java

package com.example.notificationservice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btnStart;
private Button btnStop; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
} @Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnStart) {
// 启动Service
Intent intent = new Intent();
intent.setAction("ymw.MY_SERVICE");
startService(intent);
}
if (id == R.id.btnStop) {
// 关闭Service
Intent intent = new Intent();
intent.setAction("ymw.MY_SERVICE");
stopService(intent);
}
} @Override
public void onBackPressed() {
System.exit(0);
super.onBackPressed();
} }

3.然后是消息推送服务文件 NotificationService.java

package com.example.notificationservice;

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 NotificationService extends Service { // 获取消息线程
private MessageThread messageThread = null; // 点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null; // 通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificatioManager = null; public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.ic_launcher;
messageNotification.tickerText = "新消息";
messageNotification.defaults = Notification.DEFAULT_SOUND;
messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); messageIntent = new Intent(this, MainActivity.class);
messagePendingIntent = PendingIntent.getActivity(this, 0,
messageIntent, 0); // 开启线程
messageThread = new MessageThread();
messageThread.isRunning = true;
messageThread.start(); return super.onStartCommand(intent, flags, startId);
} /**
* 从服务器端获取消息
*
*/
class MessageThread extends Thread {
// 设置是否循环推送
public boolean isRunning = true; public void run() {
// while (isRunning) {
try {
// 间隔时间
Thread.sleep(1000);
// 获取服务器消息
String serverMessage = getServerMessage();
if (serverMessage != null && !"".equals(serverMessage)) {
// 更新通知栏
messageNotification.setLatestEventInfo(
getApplicationContext(), "新消息", "您有新消息。"
+ serverMessage, messagePendingIntent);
messageNotificatioManager.notify(messageNotificationID,
messageNotification);
// 每次通知完,通知ID递增一下,避免消息覆盖掉
messageNotificationID++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
} @Override
public void onDestroy() {
// System.exit(0);
messageThread.isRunning = false;
super.onDestroy();
} /**
* 模拟发送消息
*
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage() {
return "NEWS!";
}
}

4.最后别忘了在mainfeast.xml文件中配置Service

项目代码如下链接:

http://files.cnblogs.com/_ymw/NotificationService_%E5%8D%9A%E5%AE%A2%E9%99%84%E4%BB%B6.rar

Android (Notification)消息推送机制的更多相关文章

  1. (转)iOS消息推送机制的实现

    原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...

  2. Android开发学习笔记-关于Android的消息推送以及前后台切换

    下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...

  3. APP消息推送机制的实现(PUSH)

    出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...

  4. ios消息推送机制原理与实现

    本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...

  5. MVC异步消息推送机制

    在MVC里面,有异步控制器,可以实现模拟消息推送机制功能 1.控制器要继承至AsyncController,如 public class RealTimeController : AsyncContr ...

  6. android系统下消息推送机制

    一.推送方式简介: 当前随着移动互联网的不断加速,消息推送的功能越来越普遍,不仅仅是应用在邮件推送上了,更多的体现在手机的APP上.当我们开发需要和服务器交互的应用程序时,基本上都需要获取服务器端的数 ...

  7. 5.Android消息推送机制简单例子

    1.首先布局文件xml代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...

  8. android基于XMPP的消息推送机制

    关于服务器端向Android客户端的推送,主要有三种方式:1.客户端定时去服务端取或者保持一个长Socket,从本质讲这个不叫推送,这是去服务端拽数据.但是实现简单,主要缺点:耗电等2.Google的 ...

  9. Android本地消息推送

    项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...

随机推荐

  1. Windows系统安装测试redis

    因本人电脑是windows系统,从https://github.com/ServiceStack/redis-windows下载了兼容windows系统的redis 下载后直接解压到D:\redis目 ...

  2. hbase shell出现ERROR:Can't get master address from Zookeeper;znode data==null

    hbase shell出现ERROR:Can't get master address from Zookeeper;znode data==null(ERROR:org.apache.hadoop. ...

  3. javaWEB简单商城项目

    javaWEB简单商城项目(一) 项目中使用到了上一篇博文的分页框架,还有mybatis,重点是学习mybatis.现在有些小迷茫,不知道该干啥,唉,不想那么多了,学就对了 一.项目功能结构 1.功能 ...

  4. Android FrameWork 概述

    Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...

  5. Python 模块:random 随机数生成

    Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...

  6. katalon系列二:selenium IDE的替代者——Katalon Recorder

    Katalon Recorder是和selenium IDE一样的一个浏览器插件,可以录制web上的操作并回放,但我个人感觉Katalon Recorder更好用.大家可以直接在chrome商店下载安 ...

  7. flask_入门教程之一

    一.教程涉及开发语言.脚本.框架.数据库等内容 Python + Flask + requests 通过命令安装:pip install flask 二.创建第一个flask脚本 一个最小的 Flas ...

  8. 最小化安装Linux的常用配置整理

    基于安全性考虑,将服务器进行最小化安装,毕竟软件包越少,漏洞越少,相对来说就约安全,但是最小化安装会给运维带来一些问题和不便,下面是我总结的,常见的一些配置和工具的安装,仅供各位大神参考,如有新的id ...

  9. hnust py road

    问题 C: Py Road 时间限制: 1 Sec  内存限制: 128 MB提交: 125  解决: 34[提交][状态][讨论版] 题目描述 Life is short,you need Pyth ...

  10. sublime3 Package Control和 中文安装

    sublime3中文版需要使用PackageControl,所以首先需要安装PackageControl 一.PackageControl安装: 1.点击Preferences > Browse ...