在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实现本地推送通知的更多相关文章

  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. POJ 3624 Charm Bracelet

    DP 一直是心中痛,不多说了,这个暑假就坑在这上了. 这暑假第一道DP题,01背包问题. 题意是说物品有 重量和价值 ,但你能承受的重量有限,问你能带的最大价值. 这题数组开大点,尽管不知道有啥坑点, ...

  2. oc-24-点语法

    /** 点语法的本质是方法的调用,而不是访问成员变量,当使用点语法时, 编译器会自动展开成相应的方法.切记点语法的本质是转换成相应的set和get方法, 如果没有set和get方法,则不能使用点语法. ...

  3. IOPS

    http://www.cnblogs.com/sink_cup/archive/2012/09/14/ssd_iops_sql_nosql.html http://www.techrepublic.c ...

  4. navigationController pushViewController 多次跳转后怎么返回

    使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面,这样的话,下一页面同样在NavigationViewControl ...

  5. 项目源码--Android迷幻岛屿综合游戏

    下载源码 技术要点: 1.游戏开发综合技术 2.多线程机制实现游戏逻辑 3.自定义控件,系统控件等综合图层的使用 4.图层素材动画的综合技术 5.游戏算法的实现 6. OpenGL ES的综合使用 7 ...

  6. PEAR:使用PHPDoc轻松建立你的PEAR文档

    对于一个开发人员,文档总是最感到头疼的事情之一.而且,很可能你对待文档会采取截然不同的2种态度: 当你使用别人的代码库的时候,最希望得到的是它的技术文档,尤其是当时间很紧,而你又不得不硬着头皮去读那些 ...

  7. java.util Pattern 和 Mathcer

    1.测试给定的正则表达式是否匹配输入的字符串,这里该正则表达式只使用一次 private String regex ; private String input; @Before public voi ...

  8. Parallel.ForEach() 并行循环

    现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势. 微软的并行运算平台(Microsoft’s Parallel Computing ...

  9. Oracle基础(九) Oracle的体系结构

    一.Oracle体系结构概述: Oracle的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制.要了解Oracle数据库的体系结构,必须理解Oracle系统的重要概念和主要 ...

  10. 如何使用OpenShare部署和运营企业门户

    如何使用OpenShare部署和运营企业门户 这篇Blog是偏向企业内整体门户部署和运营的指南,是偏向整体管理和规划的,并不是针对终端用户的OpenShare软件操作手册,具体的操作可以上优酷看相关视 ...