从网上查询资料学习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. phpstorm调试配置 Xdebug

    这已经楼主第二次因为phpstorm的调试配置折腾了几个小时,这次一定要记下来!!! 以Xdebug chrome浏览器为例 一:安装 JetBrains IDE Support 二:安装 Xdebu ...

  2. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable

    编译的时候出现这个,我从svn download下来的代码,运行就报这个错. 当时我还无知的大吼,怎么可能没有配置java_home, 运行了java -version 都显示出来1.8了. 后来,让 ...

  3. luogu3343 [ZJOI2015]地震后的幻想乡

    ref 前置技能是bzoj的串珠子.这种子集dp好神啊qwq. 还有这种钦定点转移子集的方法建议按这题的方法写,不要看串珠子qwq #include <iostream> #include ...

  4. Django笔记 —— 模型

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  5. java中多态的概念

    概念: 简答来说,多态(Polymorphism)是具有表现多种形态的能力的特征.    更专业的说法是:同一个实现接口,使用不同的实例而执行不同的操作. 好处: 通过多态可以减少类中代码量,可以提高 ...

  6. iOS笔记057 - UI总结03

    控制器的父子关系 1.控制器父子关系的建立原则        如果2个控制器的view是父子关系(不管是直接还是间接的父子关系),那么这2个控制器也应该为父子关系 [self.view addSubv ...

  7. Percona-Tookit工具包之pt-duplicate-key-checker

      Preface       I suppose that we have a requirement of checking out how many duplicated indexes on ...

  8. 解决idea无法下载插件的问题

    分析原因: 使用了 https 协议下载而导致的问题. 解决办法: 找到 File -> Settings -> Appearance & Behavior -> Syste ...

  9. Oracle 学习----游标(使用带参光标cursor)

    --表参照无参光标页信息 --注意:红色就是参数 declare cursor tt(pkeycode temp.keycode%type) is select name,sal from temp ...

  10. 【Python】重定向 Stream 到文件

    Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...