在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等。

经常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层可以展开的通知列表。它会时不时的提醒你什么软件该更新了,什么人发你微信消息了等。

我主要用于记录于,按时来领取体力啊,来登录领奖啊,这个在游戏中用途比较广泛

1.写一个类NotificationService继承Service

功能逻辑:启动一个线程,定时检查是否要发送领取奖励,体力了,我这边由于涉及到公司东西,写的是简单的demo,只判断了时间点,就发送通知,实际游戏中可能根据是否已经领取过,多少等级,启动多久之后等条件,有问题可以共同讨论。

代码:

  1. package com.test.service;
  2.  
  3. import java.util.Calendar;
  4. import java.util.List;
  5.  
  6. import android.R;
  7. import android.app.ActivityManager;
  8. import android.app.Notification;
  9. import android.app.NotificationManager;
  10. import android.app.PendingIntent;
  11. import android.app.Service;
  12. import android.content.ComponentName;
  13. import android.content.Context;
  14. import android.content.Intent;
  15. import android.os.IBinder;
  16. import android.util.Log;
  17.  
  18. public class NotificationService extends Service {
  19.  
  20. private static final String TAG = "TAG";
  21. private static final int CHECK_TICK = 1*60*1000;
  22. private static final int GET_STAMINA_ID = 1;
  23. private static final int PUNCH_CARD_ID = 2;
  24.  
  25. private NotificationService m_service = null;
  26. private NotificationManager m_notificationMgr = null;
  27. private NotifyThread m_notifyThread = null;
  28.  
  29. @Override
  30. public IBinder onBind(Intent intent) {
  31. // TODO Auto-generated method stub
  32. return null;
  33. }
  34.  
  35. @Override
  36. public void onCreate() {
  37. // TODO Auto-generated method stub
  38. super.onCreate();
  39.  
  40. m_service = this;
  41. m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  42.  
  43. if(m_notificationMgr == null)
  44. {
  45. Log.i(TAG, "NotificationService noticationMgr null");
  46. }
  47. m_notifyThread = new NotifyThread();
  48. m_notifyThread.start();
  49.  
  50. Log.i(TAG, "NotificationService onCreate...");
  51. }
  52.  
  53. @Override
  54. public int onStartCommand(Intent intent, int flags, int startId) {
  55. // TODO Auto-generated method stub
  56.  
  57. Log.i(TAG, "NotificationService onStartCommand...");
  58.  
  59. return super.onStartCommand(intent, flags, startId);
  60. }
  61.  
  62. @Override
  63. public void onDestroy() {
  64. // TODO Auto-generated method stub
  65.  
  66. Log.i(TAG, "NotificationService onDestroy...");
  67.  
  68. if(m_notifyThread != null)
  69. {
  70. m_notifyThread.stopThread();
  71. }
  72.  
  73. super.onDestroy();
  74. }
  75.  
  76. public void notify(int notifyId, String strTitle, String strMsg)
  77. {
  78. if(m_notificationMgr != null)
  79. {
  80. Notification localNotification = new Notification();
  81. localNotification.icon = R.id.icon;
  82. localNotification.defaults = Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND;
  83. localNotification.when = System.currentTimeMillis();
  84. localNotification.tickerText = strMsg;
  85.  
  86. ComponentName componentName = new ComponentName(this, LoadingActivity.class);
  87. Intent intent = new Intent(Intent.ACTION_MAIN);
  88. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  89. intent.setComponent(componentName);
  90. //Intent intent = new Intent(this, LoadingActivity.class);
  91. //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  92. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  93. localNotification.setLatestEventInfo(this, strTitle, strMsg, pendingIntent);
  94.  
  95. m_notificationMgr.notify(notifyId, localNotification);
  96. }
  97. }
  98.  
  99. public static boolean isRunning(Context context)
  100. {
  101. ActivityManager activityMgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
  102. if(activityMgr != null)
  103. {
  104. List<ActivityManager.RunningServiceInfo> serviceList = activityMgr.getRunningServices(50);
  105.  
  106. if(serviceList.isEmpty())
  107. {
  108. return false;
  109. }
  110.  
  111. for (int i = 0, n = serviceList.size(); i < n; ++i)
  112. {
  113. if(serviceList.get(i).service.getClassName().toString().equals("com.jthd.marsX.NotificationService"))
  114. {
  115. return true;
  116. }
  117. }
  118. }
  119.  
  120. return false;
  121. }
  122.  
  123. private class NotifyThread extends Thread
  124. {
  125. private boolean m_bStop = false;
  126.  
  127. public synchronized void stopThread()
  128. {
  129. m_bStop = true;
  130. }
  131.  
  132. @Override
  133. public void run()
  134. {
  135. Log.i(TAG, "NotifyThread run...");
  136.  
  137. while(!m_bStop)
  138. {
  139. checkNotify();
  140.  
  141. try
  142. {
  143. sleep(CHECK_TICK);
  144. }
  145. catch (InterruptedException e)
  146. {
  147. // TODO Auto-generated catch block
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. Log.i(TAG, "NotifyThread stop...");
  153. }
  154.  
  155. public void checkNotify()
  156. {
  157. Calendar cal = Calendar.getInstance();
  158. int hour = cal.get(Calendar.HOUR_OF_DAY);
  159. //int minute = cal.get(Calendar.MINUTE);
  160. //int second = cal.get(Calendar.SECOND);
  161.  
  162. //Log.i(TAG, "hour:" + hour + "m:" + minute + "s:" + second);
  163.  
  164. if((hour >= 12 && hour < 14) || (hour >= 18 && hour <20))
  165. m_service.notify(GET_STAMINA_ID, "通知", "来这领取你的体力了");
  166.  
  167. if(hour == 20)
  168. m_service.notify(PUNCH_CARD_ID, "通知", "来这签到领奖了");
  169. }
  170. }
  171. }

2.写个启动服务的代码

  1. package com.test.service;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6.  
  7. import org.cocos2dx.lib.Cocos2dxActivity;
  8.  
  9. public class GameActivity extends Cocos2dxActivity {
  10.  
  11. protected void onCreate(Bundle savedInstanceState) {
  12.  
  13. super.onCreate(savedInstanceState);
  14.  
  15. startService();
  16. }
  17.  
  18. @Override
  19. protected void onResume() {
  20. // TODO Auto-generated method stub
  21. super.onResume();
  22. }
  23.  
  24. @Override
  25. protected void onPause() {
  26. // TODO Auto-generated method stub
  27. super.onPause();
  28. }
  29.  
  30. @Override
  31. protected void onDestroy() {
  32. // TODO Auto-generated method stub
  33. super.onDestroy();
  34. }
  35.  
  36. public void startService()
  37. {
  38. if(!NotificationService.isRunning(this))
  39. {
  40. Intent startIntent = new Intent(this,
  41.         NotificationService.class);
  42. startService(startIntent);
  43. }
  44. }
  45. }

需要在AndroidManifest.xml注册这两个类,一个Activity,一个Service

最近玩的像素鸟游戏 http://aaron.apps.cn/details?app=d00d1a119cb07178245ec5ae728e34a0

采用Service实现本地推送通知的更多相关文章

  1. iOS 本地推送通知

    1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...

  2. Swift 本地推送通知UILocalNotification

    Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...

  3. (七十三)iOS本地推送通知的实现

    iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...

  4. SwiftUI - iOS10本地推送通知教程UserNotifications在Swift中的实现方式

    简介 消息推送相信在很多人的眼里都不陌生了吧?像即时聊天微信,好友发信息给你时会在顶部弹下小窗口提醒你.也像是在影院APP预订了电影票,在开场前一小时你也会收到提醒.这类推送是需要经过后端发送请求的, ...

  5. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  6. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  7. iOS 进阶---推送通知之本地通知

    1.推送通知的2种方式 1)本地推送通知(Local Notification) 2)远程推送通知(Remote Notification) 2.通知的作用 可以让不在前台运行的app,告知用户app ...

  8. 推送通知/传感器/UIDynamic仿真(推送通知已适配iOS10)

    推送通知/传感器/UIDynamic 一.推送通知 1.推送通知简介 什么是推送通知 此处的推送通知与NSNotification没有任何关系 可以理解为,向用户推送一条信息来通知用户某件事情 作用: ...

  9. 包教包会:本地推送 & 远程推送

    什么是推送?注意,和我们常用的抽象通知不同(NSNotification): 可以让不在前台运行的app,告知用户app内部发生了什么事情:或者没有运行的app接收到服务器发来的通知..比如离线QQ接 ...

随机推荐

  1. 适用于各浏览器支持图片预览,无刷新异步上传js插件

    文件上传无疑是web应用中一个非常常用的功能,不管是PHP.jsp还是aspx.mvc等都会需要文件上传,但是众所周知当使用自带的文件上传功能时总会出现页面刷新的情况.当然现在有了html5这个好东西 ...

  2. 为rm命令增加回收站功能

    rm是个强大的命令,特别是rm -rf有时候强大到让你欲哭无泪,当你想清除当前目录下的所有文件和目录时,很简单 $sudo rm -rf ./* 这没什么,但是,但是如果不小心打成这样 $sudo r ...

  3. 将一副图片编译进uboot

    在uboot显示图片的时候可以将jpg图片作为uboot的一段,在程序中访问该段,实现图片. 图片: logo.jpg ,将其拷贝到common下 修改u-boot.lds,添加".log& ...

  4. Quoit Design

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission ...

  5. 微信js获得签名signature

    服务器端: 1 获取微信js accessToken 备注:access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 开发者需要进行妥善保存.access_ ...

  6. 【阿里云产品公测】阿里云ACE配置全程图解,详细到不行!

    作者:阿里云用户sofia 看过阿里云社区的其他技术大姥们的评测教程,感觉还是不够详细,对于一个第一次接触ace.新浪sae这类的应用来说还是比较陌生的.我最喜欢写教程了,不过我有我的风格,那就是简单 ...

  7. Java POI操作Excle工具类

    用到了jxl.jar和poi.jar 一些基本的操作Excel的操作方法: import java.io.File; import java.io.FileInputStream; import ja ...

  8. 大部分人努力程度之低,根本轮不到拼天赋 [转自w3cschool]

    2014-05-31 w3cschool 在过去的三个多月里,每周六一天的心理咨询师的培训课成了我一周中最重要最开心的事情之一.因为国庆节的缘故,从9月中旬到10月中旬培训中心都没有安排课程,因此习惯 ...

  9. Angularjs 中使用指令绑定点击事件

    项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...

  10. Sqlite3笔记

    .tables 查看表.databases 创建数据库alter table 表名 RENAME TO 新表名ALTER TABLE 表名 add column 列名 datatype [DEFAUL ...