消息推送机制原理
2016-04-22 18:38
337 查看
在现在的android开发中,为了提高用户 的体验,我们会经常使用到消息推送这样一个功能,但是在一般的开发中我们通常采用极光推送等第三方的框架,来进行推送,那么google本身的推送的原理是什么呢?
首先我们可以定义一个布局文件里面含有两个按钮,其中一个按钮是启动,另一个是停止;具体的就不做详细的解答(相信大家都能做到这个功能),那么在MainActivity中的onCreate()里面设置两个按钮的点击事件,启动和停止具体的代码如下所示:
以上是界面的相关逻辑,最主要的是在消息推送的服务类的逻辑的书写
书写一个类继承自service这个类,然后覆写其中的相关方法 onBind
onStartCommand
onDestroy
其中最主要的逻辑就是在onStartCommand中进行开启一个子线程,然后从服务端获取消息以后并且推送的逻辑,相关的代码如下所示:
注意:在进行相关的操作的时候,千万不要忘记在mainfest.xml文件中进行设置注册!!
首先我们可以定义一个布局文件里面含有两个按钮,其中一个按钮是启动,另一个是停止;具体的就不做详细的解答(相信大家都能做到这个功能),那么在MainActivity中的onCreate()里面设置两个按钮的点击事件,启动和停止具体的代码如下所示:
@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("zwk.MY_SERVICE"); startService(intent); } if (id == R.id.btnStop) { // 关闭Service Intent intent = new Intent(); intent.setAction("zwk.MY_SERVICE"); stopService(intent); } } @Override public void onBackPressed() { System.exit(0); super.onBackPressed(); }
以上是界面的相关逻辑,最主要的是在消息推送的服务类的逻辑的书写
书写一个类继承自service这个类,然后覆写其中的相关方法 onBind
onStartCommand
onDestroy
其中最主要的逻辑就是在onStartCommand中进行开启一个子线程,然后从服务端获取消息以后并且推送的逻辑,相关的代码如下所示:
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!"; }
注意:在进行相关的操作的时候,千万不要忘记在mainfest.xml文件中进行设置注册!!
相关文章推荐
- AES加密
- IDEA 回滚SVN更新内容
- 制作一个属于自己的CocoaPods文件!
- 快速幂
- 快速幂
- Java - zookeeper 服务注册发现
- cisco的模拟器上完成单臂路由配置的试验
- Android开发相关的Blog推荐
- 九度OJ1486 /POJ 1029/2012北京大学研究生复试上机
- 团队开发之个人博客三
- CentOS虚拟机与本机同步时间
- Adobe源码泄漏?3行代码搞定,Flash动画无缝导入Android/iOS/cocos2dx(一)
- 一些思路
- linux下实现两个线程之间的通信
- centos7 nginx用systemctl方式管理的脚本
- POJ 2991 Crane 线段树
- 杭电1418
- mac 常用快捷键1:
- idea 快捷键1
- BaseRecyclerAdapter之添加不同布局(优化篇)