从网上查询资料学习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. border与background定位

    1.background定位的局限 只能相对于左上角数值定位,不能相对于右下 即background-position默认相对于左上方定位的 2.怎样让图片相对于右下角? background-pos ...

  2. JavaScript - 问题集 - 含function的json对象与json字符串之间相互转换

    基本的转换为:JSON.parse与JSON.stringify. 但是json数据中含function,则转换后,function会丢失,如: var json={ test:'test', log ...

  3. 我的转行之路(电气转IT)------2018阿里校招面经

    博主本专业电气,今年3月下定决心转向互联网行业,本来想依仗自己比较自信的学习能力自学成才的,不过学了一段时间感觉还是需要一个人来指点,不仅仅是指点一些技术性的问题,更是需要有人来指点一下方向性的问题. ...

  4. HTML5技巧

    HTML5技巧   HTML5 技巧一:当今科技发展的速度真惊人,稍不留神,就可能无法跟上它的步伐.新一代的HTML-HTML5的发展也不停的带给我们新的惊喜,我们将通过这篇文章向大家介绍一些HTML ...

  5. 《Cracking the Coding Interview》——第7章:数学和概率论——题目3

    2014-03-20 02:05 题目:给定笛卡尔二维平面上两条直线,判断它们是否相交. 解法:相交.重合.平行. 代码: // 7.3 Given two lines on the Cartesia ...

  6. python 3 直接使用reload函数报错

    reload()是python2 的内置函数可以直接使用,但是python3 直接使用此函数报错,需要导入importlib 模块 from importlib import reload

  7. 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)

    链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...

  8. CentOS 6.3下安装Vsftp,虚拟用户

    CentOS 6.3下安装Vsftp,虚拟用户一.安装:1.安装Vsftpd服务相关部件:[root@linuxidc.com ~]# yum install vsftpd*Dependencies ...

  9. Python读写tap设备

    #!/usr/bin/python import os import struct import fcntl import binascii TUNSETIFF = 0x400454ca IFF_TA ...

  10. Vuex, api, SSR, module

    vuex https://vuex.vuejs.org/zh/guide/actions.html 单向数据流 单例模式 & 多个组件共享状态 State & 状态注入 Vue.use ...