5.Android消息推送机制简单例子

1.首先布局文件xml代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.riger.notificationdemo.MainActivity">
11
12     <LinearLayout
13         android:orientation="vertical"
14         android:layout_width="match_parent"
15         android:layout_height="match_parent"
16         android:layout_alignParentTop="true"
17         android:layout_alignParentLeft="true"
18         android:layout_alignParentStart="true">
19
20         <TextView
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="hello"
24             android:id="@+id/textView" />
25
26         <Button
27             android:layout_width="match_parent"
28             android:layout_height="wrap_content"
29             android:text="通知"
30             android:id="@+id/btnStart" />
31
32         <Button
33             android:layout_width="match_parent"
34             android:layout_height="wrap_content"
35             android:text="清除"
36             android:id="@+id/btnStop" />
37     </LinearLayout>
38 </RelativeLayout>

2.主界面代码MainActivity:

 1 public class MainActivity extends Activity implements View.OnClickListener{
 2
 3     private Button btnStart;
 4     private Button btnStop;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         initView();
10     }
11
12     public void initView(){
13         btnStart = (Button)findViewById(R.id.btnStart);
14         btnStop = (Button)findViewById(R.id.btnStop);
15         btnStart.setOnClickListener(this);
16         btnStop.setOnClickListener(this);
17     }
18
19
20     @Override
21     public void onClick(View view) {
22         int id = view.getId();
23         switch(id){
24             case R.id.btnStart:
25                 //启动服务
26                 Intent intent = new Intent();
27                 intent.setAction("ymw.MY_SERVICE");
28                 intent.setPackage("com.example.riger.notificationdemo");
29                 startService(intent);
30                 break;
31             case R.id.btnStop:
32                 Intent i = new Intent();
33                 i.setAction("ymw.MY_SERVICE");
34                 i.setPackage("com.example.riger.notificationdemo");
35                 stopService(i);
36                 break;
37             default:
38                 break;
39         }
40
41     }
42 }

3.消息推送服务文件NotificationService

 1 public class NotificationService extends Service{
 2
 3     private static final String TAG = NotificationService.class.getSimpleName();
 4     //获取消息线程
 5     private MessageThread messageThread = null;
 6
 7     //点击查看
 8     private Intent messageIntent = null;
 9     private PendingIntent messagePendingIntent = null;
10
11     //通知栏消息
12     private NotificationManager messageNotificationManage = null;
13
14     @Nullable
15     @Override
16     public IBinder onBind(Intent intent) {
17         return null;
18     }
19
20     @Override
21     public int onStartCommand(Intent intent, int flags, int startId) {
22         // 初始化
23         messageNotificationManage = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
24
25         messageIntent = new Intent(this,MainActivity.class);
26         messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0);
27
28         // 开启线程
29         messageThread = new MessageThread();
30         messageThread.isRunning = true;
31         messageThread.start();
32
33         return super.onStartCommand(intent,flags,startId);
34     }
35
36     /**
37      * 从服务器获取信息
38      */
39     class MessageThread extends Thread{
40         public boolean isRunning = false;
41         public void run() {
42             try{
43                 Log.v(TAG,"111111111");
44                 // 间隔时间
45                 Thread.sleep(1000);
46                 // 获取服务器消息
47                 String serverMessage = getServerMessage();
48                 if (serverMessage != null && !"".equals(serverMessage)) {
49                     Log.v(TAG,"22222222");
50                     // 更新通知栏
51                     Notification.Builder builder = new Notification.Builder(NotificationService.this);
52                     builder.setSmallIcon(R.mipmap.ic_launcher);
53                     builder.setTicker("显示第一个通知");
54                     builder.setContentTitle("第一个通知");
55                     builder.setContentText("Hello World!");
56                     builder.setWhen(System.currentTimeMillis()); //发送时间
57                     builder.setDefaults(Notification.DEFAULT_ALL);
58                     Notification notification = builder.build();
59                     messageNotificationManage.notify(123, notification);
60                     Log.v(TAG,"33333333");
61                 }
62
63             }catch (InterruptedException e) {
64                 e.printStackTrace();
65             }
66         }
67     }
68
69     @Override
70     public void onDestroy(){
71         messageThread.isRunning = false;
72         super.onDestroy();
73     }
74
75     /**
76      * 模拟发送信息
77      */
78     public String getServerMessage() {
79         return "HELLO WORLD!";
80     }
81
82 }

最后记得配置下Service:

1 <service android:name=".NotificationService">
2             <intent-filter>
3                 <action android:name="ymw.MY_SERVICE"/>
4             </intent-filter>
5         </service>

运行效果:

      

时间: 2024-08-06 16:01:17

5.Android消息推送机制简单例子的相关文章

Android消息推送机制

1.推送方式基础知识: 当我们开发需要和服务器交互的应用程序时,基本上都需要获取服务器端的数据,比如<地震应急通>就需要及时获取服务器上最新的地震信息.要获取服务器 上不定时更新的信息一般来说有两种方法,第一种是客户端使用Pull(拉)的方式,隔一段时间就去服务器上获取信息,看是否有更新的信息出现.第二种就是 服务器使用Push(推送)的方式,当服务器端有新信息了,则把最新的信息Push到客户端上.? 虽然Pull和Push两种方式都能实现获取服务器端更新信息的功能,但是明显来说Push is

Android (Notification)消息推送机制

从网上查询资料学习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与iOS系统的消息推送机制

相信大家在使用iPhone版微信的时候都会有这样的经历,微信已经处于关闭状态了(后台进程运行一段时间就被系统杀掉),这时候我们收到了一个消息提醒,打开微信应用,微信显示“连接中…”和“收取中…”,然后再次显示一次刚才系统推送给我的消息通知.对这个现象比较好奇,于是去知乎上查一下资料,发现知乎上的热心人还真多,看了大家的回答之后,总结如下: [之所以去知乎查看技术问题,因为我并非技术人员,而知乎上很多开发人员是会用通俗易懂的方式解释好技术问题的,因为里面有不少大牛.] 先介绍一下两个重要的消息推送

Android消息推送完美解决方案全析

推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来和大家共同探讨一种Android消息推送的完美解决方案. 一.消息推送基础 消息推送,就是在互联网上通过定期传送用户需要的信息来减少信息过载的一项新技术.推送技术通过自动传送信息给用户,来减少用于网络上搜索的时间.它根据用户的兴趣来搜索.过滤信息,并将其定期推给用户,帮助用户高效率地发掘有价值的信息 当我们开发需要和服务器交互的移动应用

Android消息推送:手把手教你集成小米推送

前言 在Android开发中,消息推送功能的使用非常常见. 为了降低开发成本,使用第三方推送是现今较为流行的解决方案. 今天,我将手把手教大家如何在你的应用里集成小米推送 该文档基于小米推送官方Demo,并给出简易推送Demo 看该文档前,请先阅读我写的另外两篇文章: 史上最全解析Android消息推送解决方案 Android推送:第三方消息推送平台详细解析 目录 1. 官方Demo解析 首先,我们先对小米官方的推送Demo进行解析. 请先到官网下载官方Demo和SDK说明文档 1.1 Demo

API 23之消息推送机制

作为一个菜鸟,每个星期写一篇随笔来记录这周所学是很有必要的: 1.gson解析json,重点在于构建json数据的实体类,不过现在android studio里有gson Formart这个快速构建实体类的工具了,所以比较容易上手. 2.thinkandroid框架中的联网请求,同步(SyncHttpClient),异步(AsyncHttpClient),但可能有点过时了,毕竟这个框架几年都没有更新维护了. 3.自定义空间及属性. 4.glide图片加载缓存,okhttp联网请求及其封装的类Ok

APP消息推送机制的实现(PUSH)

出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/article/details/40898183 2.http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html 以下一篇是关于android的 3.http://www.cnblogs.com/wxishang1991/p/521940

iPhone消息推送机制实现与探讨

最近两天在研究ios的消息推送机制.研究这个东西,还是充满兴趣的. Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器. 上图可以分为三个阶段. 第一阶段:.net应用程序把要发送的消息.目的iPhone的标识打包,发给APNS. 第二阶段:APNS在

苹果apns消息推送机制

苹果apns消息推送机制[电薇:132乄8688乄4109][Q群780516296]声声叹!鲁能球迷被恒大踢服了 保住前三依旧是目标Space X发推秀宇航员进出臂 设计科幻美"力挺"被"断交"的台湾 马英九讽:无说服力深度|中日游泳PK中国差在哪?个别高手长期未突破日本防卫省敲定2019预算申请 总额5.2986万亿日-传软银决定不向电动汽车企业蔚来进行投资围堵P2P恶意逃废债 纳入征信"进行时"大连瓦房店市委书记被查 曾批落马前同僚教训深刻