5.Android消息推送机制简单例子
1.首先布局文件xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.riger.notificationdemo.MainActivity"> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/textView" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通知"
android:id="@+id/btnStart" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:id="@+id/btnStop" />
</LinearLayout>
</RelativeLayout>
2.主界面代码MainActivity:
public class MainActivity extends Activity implements View.OnClickListener{
private Button btnStart;
private Button btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public 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 view) {
int id = view.getId();
switch(id){
case R.id.btnStart:
//启动服务
Intent intent = new Intent();
intent.setAction("ymw.MY_SERVICE");
intent.setPackage("com.example.riger.notificationdemo");
startService(intent);
break;
case R.id.btnStop:
Intent i = new Intent();
i.setAction("ymw.MY_SERVICE");
i.setPackage("com.example.riger.notificationdemo");
stopService(i);
break;
default:
break;
}
}
}
3.消息推送服务文件NotificationService
public class NotificationService extends Service{
private static final String TAG = NotificationService.class.getSimpleName();
//获取消息线程
private MessageThread messageThread = null;
//点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private NotificationManager messageNotificationManage = null;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
messageNotificationManage = (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 = false;
public void run() {
try{
Log.v(TAG,"111111111");
// 间隔时间
Thread.sleep(1000);
// 获取服务器消息
String serverMessage = getServerMessage();
if (serverMessage != null && !"".equals(serverMessage)) {
Log.v(TAG,"22222222");
// 更新通知栏
Notification.Builder builder = new Notification.Builder(NotificationService.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("显示第一个通知");
builder.setContentTitle("第一个通知");
builder.setContentText("Hello World!");
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
messageNotificationManage.notify(123, notification);
Log.v(TAG,"33333333");
}
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy(){
messageThread.isRunning = false;
super.onDestroy();
}
/**
* 模拟发送信息
*/
public String getServerMessage() {
return "HELLO WORLD!";
}
}
最后记得配置下Service:
<service android:name=".NotificationService">
<intent-filter>
<action android:name="ymw.MY_SERVICE"/>
</intent-filter>
</service>
运行效果:

5.Android消息推送机制简单例子的更多相关文章
- Android (Notification)消息推送机制
从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...
- Android消息推送完美解决方案全析
推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来和大家共同探讨一种Android消息推 ...
- Android消息推送完美方案[转]
转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...
- Android消息推送完美方案
转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...
- Android消息推送的服务端
2.Android消息推送 MQTT服务器采用mosquito http://mosquitto.org/ PHP管理包采用phpmqttclient:https://github.com/toku ...
- (转)iOS消息推送机制的实现
原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...
- APP消息推送机制的实现(PUSH)
出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...
- ios消息推送机制原理与实现
本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
随机推荐
- 【代码笔记】iOS-获取字符串的宽度,高度
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- Feathers组件的宽度或高度属性,为什么我得到的值是0
Feathers组件使用一个失效系统延迟一会儿繁重的重绘,这样你可以在一个时间内改变多个属性.如果你还没有明确地设置宽度和高度,他们会自动 调整自身到一套“理想”的尺度.然而,这并不会发生,直到他们验 ...
- IT人经济思维之投资 - 创业与投资系列文章
前面笔者写过一个文(IT从业者的职业规划),主要通过笔者的从业道路的经验,介绍了IT从业者的职业选择道路问题,主要从技术.业务和管理三大方面进行了描述.然后,通过文(IT从业者的职业道路(从程序员到部 ...
- Google在KDD2013上关于CTR的一篇论文
最近在做CTR,刚好Google在KDD发了一篇文章,讲了他们的一些尝试,总结一下: 先是一些公式的符号说明: 一.优化算法 CTR中经常用Logistic regression进行训练,一个常用的L ...
- 使用Navicat连接Oracle数据时的一些问题,连接时错误:ORA-28547,新建用户后连接时错误:ORA-01017
发现Navicat作为一款连接数据库的工具确实很好用,可视化的界面看着舒服很多,下面说说我今天在连接Oracle数据库时的一些问题: 1.安装好Oracle后直接用Navicat是连接不上的,会出现如 ...
- PHP的openssl加密扩展使用小结
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- redis 集群创建常见几个问题
Redis配置集群遇到问题及解决方法 配置完所有主节点后,报" ERR Invalid node address specified" 由于Redis-trib.rb 对域名或 ...
- 【Linux】解决Wesnoth中文乱码问题
现在使用的系统是Linux Mint 18,安装了Wesnoth,发现打开之后部分中文显示正常,部分中文显示为乱码. 谷歌出的很多办法都只给出了几条指令,但并不具有普适性,我提供一种新的方法,通过修改 ...
- 4412开发板搭建Uboot、Kernel和Android4.0的编译环境方法
本文转自迅为4412开发板实战教程书籍:http://www.topeetboard.com 迅为是基于Ubuntu12.04.2平台做开发,所有的配置和编译脚本也是基于此平台,没有在其它平台上测试过 ...
- 报表软件FineReport如何连接SAP HANA
1. 环境搭建 1.1 环境准备 首先确认HANA Studio的环境是否允许工程进行NewFile的操作,不行的话要考虑更新Studio的版本. HANAStudio需要依赖Java jdk1.6或 ...