采用Service实现本地推送通知
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等。
经常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层可以展开的通知列表。它会时不时的提醒你什么软件该更新了,什么人发你微信消息了等。
我主要用于记录于,按时来领取体力啊,来登录领奖啊,这个在游戏中用途比较广泛
1.写一个类NotificationService继承Service
功能逻辑:启动一个线程,定时检查是否要发送领取奖励,体力了,我这边由于涉及到公司东西,写的是简单的demo,只判断了时间点,就发送通知,实际游戏中可能根据是否已经领取过,多少等级,启动多久之后等条件,有问题可以共同讨论。
代码:
- package com.test.service;
- import java.util.Calendar;
- import java.util.List;
- import android.R;
- import android.app.ActivityManager;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class NotificationService extends Service {
- private static final String TAG = "TAG";
- private static final int CHECK_TICK = 1*60*1000;
- private static final int GET_STAMINA_ID = 1;
- private static final int PUNCH_CARD_ID = 2;
- private NotificationService m_service = null;
- private NotificationManager m_notificationMgr = null;
- private NotifyThread m_notifyThread = null;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- m_service = this;
- m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- if(m_notificationMgr == null)
- {
- Log.i(TAG, "NotificationService noticationMgr null");
- }
- m_notifyThread = new NotifyThread();
- m_notifyThread.start();
- Log.i(TAG, "NotificationService onCreate...");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- Log.i(TAG, "NotificationService onStartCommand...");
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- Log.i(TAG, "NotificationService onDestroy...");
- if(m_notifyThread != null)
- {
- m_notifyThread.stopThread();
- }
- super.onDestroy();
- }
- public void notify(int notifyId, String strTitle, String strMsg)
- {
- if(m_notificationMgr != null)
- {
- Notification localNotification = new Notification();
- localNotification.icon = R.id.icon;
- localNotification.defaults = Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND;
- localNotification.when = System.currentTimeMillis();
- localNotification.tickerText = strMsg;
- ComponentName componentName = new ComponentName(this, LoadingActivity.class);
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.setComponent(componentName);
- //Intent intent = new Intent(this, LoadingActivity.class);
- //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- localNotification.setLatestEventInfo(this, strTitle, strMsg, pendingIntent);
- m_notificationMgr.notify(notifyId, localNotification);
- }
- }
- public static boolean isRunning(Context context)
- {
- ActivityManager activityMgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
- if(activityMgr != null)
- {
- List<ActivityManager.RunningServiceInfo> serviceList = activityMgr.getRunningServices(50);
- if(serviceList.isEmpty())
- {
- return false;
- }
- for (int i = 0, n = serviceList.size(); i < n; ++i)
- {
- if(serviceList.get(i).service.getClassName().toString().equals("com.jthd.marsX.NotificationService"))
- {
- return true;
- }
- }
- }
- return false;
- }
- private class NotifyThread extends Thread
- {
- private boolean m_bStop = false;
- public synchronized void stopThread()
- {
- m_bStop = true;
- }
- @Override
- public void run()
- {
- Log.i(TAG, "NotifyThread run...");
- while(!m_bStop)
- {
- checkNotify();
- try
- {
- sleep(CHECK_TICK);
- }
- catch (InterruptedException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- Log.i(TAG, "NotifyThread stop...");
- }
- public void checkNotify()
- {
- Calendar cal = Calendar.getInstance();
- int hour = cal.get(Calendar.HOUR_OF_DAY);
- //int minute = cal.get(Calendar.MINUTE);
- //int second = cal.get(Calendar.SECOND);
- //Log.i(TAG, "hour:" + hour + "m:" + minute + "s:" + second);
- if((hour >= 12 && hour < 14) || (hour >= 18 && hour <20))
- m_service.notify(GET_STAMINA_ID, "通知", "来这领取你的体力了");
- if(hour == 20)
- m_service.notify(PUNCH_CARD_ID, "通知", "来这签到领奖了");
- }
- }
- }
2.写个启动服务的代码
- package com.test.service;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import org.cocos2dx.lib.Cocos2dxActivity;
- public class GameActivity extends Cocos2dxActivity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- startService();
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- }
- public void startService()
- {
- if(!NotificationService.isRunning(this))
- {
- Intent startIntent = new Intent(this,
- NotificationService.class);
- startService(startIntent);
- }
- }
- }
需要在AndroidManifest.xml注册这两个类,一个Activity,一个Service
最近玩的像素鸟游戏 http://aaron.apps.cn/details?app=d00d1a119cb07178245ec5ae728e34a0
采用Service实现本地推送通知的更多相关文章
- iOS 本地推送通知
1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...
- Swift 本地推送通知UILocalNotification
Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...
- (七十三)iOS本地推送通知的实现
iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...
- SwiftUI - iOS10本地推送通知教程UserNotifications在Swift中的实现方式
简介 消息推送相信在很多人的眼里都不陌生了吧?像即时聊天微信,好友发信息给你时会在顶部弹下小窗口提醒你.也像是在影院APP预订了电影票,在开场前一小时你也会收到提醒.这类推送是需要经过后端发送请求的, ...
- iOS 通知、本地通知和推送通知有什么区别? APNS机制。
本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- iOS 进阶---推送通知之本地通知
1.推送通知的2种方式 1)本地推送通知(Local Notification) 2)远程推送通知(Remote Notification) 2.通知的作用 可以让不在前台运行的app,告知用户app ...
- 推送通知/传感器/UIDynamic仿真(推送通知已适配iOS10)
推送通知/传感器/UIDynamic 一.推送通知 1.推送通知简介 什么是推送通知 此处的推送通知与NSNotification没有任何关系 可以理解为,向用户推送一条信息来通知用户某件事情 作用: ...
- 包教包会:本地推送 & 远程推送
什么是推送?注意,和我们常用的抽象通知不同(NSNotification): 可以让不在前台运行的app,告知用户app内部发生了什么事情:或者没有运行的app接收到服务器发来的通知..比如离线QQ接 ...
随机推荐
- 适用于各浏览器支持图片预览,无刷新异步上传js插件
文件上传无疑是web应用中一个非常常用的功能,不管是PHP.jsp还是aspx.mvc等都会需要文件上传,但是众所周知当使用自带的文件上传功能时总会出现页面刷新的情况.当然现在有了html5这个好东西 ...
- 为rm命令增加回收站功能
rm是个强大的命令,特别是rm -rf有时候强大到让你欲哭无泪,当你想清除当前目录下的所有文件和目录时,很简单 $sudo rm -rf ./* 这没什么,但是,但是如果不小心打成这样 $sudo r ...
- 将一副图片编译进uboot
在uboot显示图片的时候可以将jpg图片作为uboot的一段,在程序中访问该段,实现图片. 图片: logo.jpg ,将其拷贝到common下 修改u-boot.lds,添加".log& ...
- Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission ...
- 微信js获得签名signature
服务器端: 1 获取微信js accessToken 备注:access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 开发者需要进行妥善保存.access_ ...
- 【阿里云产品公测】阿里云ACE配置全程图解,详细到不行!
作者:阿里云用户sofia 看过阿里云社区的其他技术大姥们的评测教程,感觉还是不够详细,对于一个第一次接触ace.新浪sae这类的应用来说还是比较陌生的.我最喜欢写教程了,不过我有我的风格,那就是简单 ...
- Java POI操作Excle工具类
用到了jxl.jar和poi.jar 一些基本的操作Excel的操作方法: import java.io.File; import java.io.FileInputStream; import ja ...
- 大部分人努力程度之低,根本轮不到拼天赋 [转自w3cschool]
2014-05-31 w3cschool 在过去的三个多月里,每周六一天的心理咨询师的培训课成了我一周中最重要最开心的事情之一.因为国庆节的缘故,从9月中旬到10月中旬培训中心都没有安排课程,因此习惯 ...
- Angularjs 中使用指令绑定点击事件
项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...
- Sqlite3笔记
.tables 查看表.databases 创建数据库alter table 表名 RENAME TO 新表名ALTER TABLE 表名 add column 列名 datatype [DEFAUL ...