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 完整的 ...
随机推荐
- ObjectAnimator.start()工作原理
分析下面一段代码的逻辑 objectAnimator.start(); 他会调用父类的start(),即ValueAnimator,我们分析valueAnimator.start()即可 ValueA ...
- break、continue、return
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- RedHat 4下无resize2fs命令
在Red Hat Enterprise Linux AS release 4上进行LVM扩展分区时,发现RedHat 4下没有resize2fs,不过可以用ext2online替换resize2fs. ...
- CLR via C# 读书笔记---常量、字段、方法和参数
常量 常量是值从不变化的符号.定义常量符号时,它的值必须能在编译时确定.确定后,编译器将唱两只保存在程序集元数据中.使用const关键字声明常量.由于常量值从不变化,所以常量总是被视为类型定义的一部分 ...
- 【转】Java并发编程:深入剖析ThreadLocal
来自: http://www.importnew.com/17849.html 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本 ...
- diff/merge configuration in Team Foundation - common Command and Argument values - MSDN Blogs
One of the extensibility points we have in Team Foundation V1 is that you can configure any other di ...
- Linux下5种IO模型的小结
概述 接触网络编程,我们时常会与各种与IO相关的概念打交道:同步(Synchronous).异步(ASynchronous).阻塞(blocking)和非阻塞(non-blocking).关于概念的区 ...
- .Net 扩展方法集合.
在项目中很多时候都会对字符串和集合做特定的处理.而且很多地方都会去调用.为了解决这些问题.我们通常会在项目中吧这些方法提成公共方法.下面是自己总结的项目中用到的一些扩展方法.封装成了一个Libra ...
- [普通平衡树splay]【学习笔记】
参考: http://blog.csdn.net/clove_unique/article/details/50630280 gty课件 找一个好的风格太难了,自己习惯用struct,就强行用stru ...
- Java 读写文件方案
一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println ...