android实现通知栏消息
一、原理
消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来;另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息。
二、步骤(代码)
注: Notification //是具体状态栏对象,设置Icon、文字、声音等。
NotificationMangager //状态栏通知管理类、负责发消息、清理消息。
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.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.widget.RemoteViews;
/**
* 消息推送
*
* @author Msquirrel
*
*/
public class MessageService extends Service {
private String TAG = "-----------";
private MessageThread messageThread = null;
// 点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
// 通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null; // 是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
private NotificationManager messageNotificatioManager = null; // 是状态栏通知的管理类,负责发通知、清楚通知等。
private RemoteViews contentView = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
// messageNotification = new
// Notification(R.drawable.icon,"新消息11",System.currentTimeMillis());//*简单消息版本里的*此版本不使用
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon;// 状态栏提示图标
messageNotification.tickerText = "嘿嘿,测试消息推送";// 状态栏提示消息
contentView = new RemoteViews(getPackageName(), R.layout.view);// 消息内容容器
contentView.setImageViewResource(R.id.image, R.drawable.icon);// 消息容器界面的图标
messageNotification.contentView = contentView;// 把消息容器和消息绑定
// messageNotification.icon = R.drawable.icon;//*简单消息版本里的*此版本不使用
// messageNotification.tickerText = "新消息11";//*简单消息版本里的*此版本不使用
// messageNotification.when=System.currentTimeMillis();
// //*简单消息版本里的*此版本不使用
// messageNotification.defaults |= Notification.DEFAULT_SOUND;//声音
// messageNotification.defaults |= Notification.DEFAULT_LIGHTS;//灯
// messageNotification.defaults |= Notification.DEFAULT_VIBRATE;//震动
// messageNotification.sound = Uri.parse("file:///sdcard/to.mp3");
messageNotification.sound = Uri.withAppendedPath(
Audio.Media.INTERNAL_CONTENT_URI, "2");// 选音乐清单的第2首歌做消息声音
// messageNotification.ledARGB = 0xff00ff00;//灯的颜色
// messageNotification.ledOnMS = 300; //亮的时间
// messageNotification.ledOffMS = 1000; //灭的时间
// messageNotification.flags |= Notification.FLAG_SHOW_LIGHTS;//显示灯
// long v[]= {0,100,200,300}; //震动频率
// messageNotification.vibrate = v;
//
messageNotification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击消息后,该消息自动退出
messageNotification.flags |= Notification.FLAG_ONGOING_EVENT;// 在上方运行消息栏中出现
// messageNotification.flags|=Notification.FLAG_NO_CLEAR;//此消息不会被清除
messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
messageIntent = new Intent(this, ShowMessage .class);// 点击消息后,要跳转的界面 ( 对应 详细消息的界面 )
// 开启线程
messageThread = new MessageThread();// 该线程每10秒,发布一条消息出来
messageThread.isRunning = true;// 设置为false后,线程跳出循环并结束对
messageThread.start();
Log.i(TAG, "startCommand");
return super.onStartCommand(intent, flags, startId);
}
/**
* 从服务器端获取消息
*/
class MessageThread extends Thread {
// 设置为false后,线程跳出循环并结束
public boolean isRunning = true;
public void run() {
while (isRunning) {
try {
String serverMessage = getServerMessage();
if (serverMessage != null && !"".equals(serverMessage)) {
// 更新通知栏
// messageNotification.setLatestEventInfo(MessageService.this,"新消息","哇~有 新消息耶!"+serverMessage,messagePendingIntent);//*简单消息版本里的*此版本不使用
contentView.setTextViewText(R.id.text, serverMessage);// 设置消息内容
messageIntent.putExtra("message", serverMessage);// 为意图添加参数
messagePendingIntent = PendingIntent.getActivity(
MessageService.this, 0, messageIntent,
PendingIntent.FLAG_CANCEL_CURRENT);// 将意图装入 延迟意图
messageNotification.contentIntent = messagePendingIntent;// 将延迟意图装入消息
messageNotificatioManager.notify(messageNotificationID,
messageNotification);// 启动Notification
Log.i(TAG, "发出消息");
// messageNotificatioManager.cancel(messageNotificationID-1);//新消息来后,消除之前的一条消息(只显示最新消息)
// 配置好下条消息的id号
messageNotificationID++;
}
// 休息10秒钟
Thread.sleep(10000);
// 获取服务器消息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 模仿服务器发送过来的消息,仅作示例
*
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage() {
Log.i(TAG, "getmessage");
return "亲, 测试成功啦~~!";
}
@Override
public void onDestroy() {
// System.exit(0);
messageThread.isRunning = false;
// 或者,二选一,推荐使用System.exit(0),这样进程退出的更干净
// messageThread.isRunning = false;
super.onDestroy();
Log.i(TAG, "destroy");
}
}
android实现通知栏消息的更多相关文章
- React Native之通知栏消息提示(android)
React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...
- Android监听消息通知栏点击事件
Android监听消息通知栏点击事件 使用BroadCastReceiver 1 新建一个NotificationClickReceiver 类,并且在清单文件中注册!! public class N ...
- Android:通知栏的使用
非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. ...
- React Native之通知栏消息提示(ios)
React Native之通知栏消息提示(ios) 一,需求分析与概述 详情请查看:React Native之通知栏消息提示(android) 二,极光推送注册与集成 2.1,注册 详情请查看:Rea ...
- 浅析Android中的消息机制(转)
原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: public class ...
- Android核心分析 之十一Android GWES之消息系统
Android GWES之Android消息系统 ...
- 浅析Android中的消息机制(转)
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 浅析Android中的消息机制
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
随机推荐
- Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs
http://codeforces.com/contest/476/problem/A A. Dreamoon and Stairs time limit per test 1 second memo ...
- 题解 P5082 【成绩】
随机跳题跳到了这一题,一看是个红题,本蒟蒻就 艰难地思考起来 高兴地写起来 这题实在不能用数组,用了数组就RE 一开始就卡在这上面了 说实话,这道题真的 很难 不算很难,只要照着公式往上面套就行了 废 ...
- ios之UITabelViewCell的自定义(xib实现2)
上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格.不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图.添加子视 ...
- 浅谈 Swift 2 中的 Objective-C 指针
浅谈 Swift 2 中的 Objective-C 指针 2015-09-07 499 文章目录 1. 在 Swift 中读 C 指针 2. 在 Swift 中创建 C 指针 3. 总结 作者:Ja ...
- Web字节码(WebAssembly) Emscripten编译器安装
首先你需要提前安装 git python 环境并且Ctrl+R输入cmd在windows的dos界面下能够运行 第一步: 在github上downloade下来emsdk git clone http ...
- PAT 乙级 1012
题目 题目地址:PAT 乙级 1012 思路 最后一个测试点怎么也过不了,问题在于A2的判断,不能单纯地以0作为判断条件:假设满足A2条件的只有两个数6和6,计算结果仍然是0,但是输出A2的值是0不是 ...
- UVa-12096-集合栈计算机
这题的话,我们读入操作之后,首先对于空集就是初始化为空. 我们可以使用typedef 对于 set 重命名为Set,这样就可以直接用Set()的语法进行空集的初始化了. 这题主要是对于集合的处理,我们 ...
- Flux reference
https://facebook.github.io/flux/docs/dispatcher.html#content 首先安装 npm install --save flux Dispatcher ...
- perl学习之正则表达式
9 Perl 中的正则表达式 正则表达式的三种形式 正则表达式中的常用模式 正则表达式的 8 大原则 正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果大家能够很 ...
- python插件,pycharm基本用法,markdown文本编写,jupyter notebook的基本操作汇总
5.14自我总结 一.python插件插件相关技巧汇总 安装在cmd上运行 #比如安装 安装:wxpy模块(支持 Python 3.4-3.+ 以及 2.7 版本):pip3 install wxpy ...