在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. j疑难杂症:ava.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finder.ClassFinder.<init>

    严重: Exception starting filter struts2java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finde ...

  2. C# 网络通信大小端转换类

    本篇文章主要介绍了"C# 网络通信大小端转换类" using System;namespace Framework.NetPackage.Common { /// <summ ...

  3. ClamAV

    ClamAV 简介以及适用范围 ClamAV是一个在命令行下查毒软件,因为它不将杀毒作为主要功能,默认只能查出您计算机内的病毒,但是无法清除,至多删除文件.ClamAV可以工作很多的平台上,但是有少数 ...

  4. IOS webView快照

    这个功能就是对网页的存储,存储成png格式的图片 且不失真 很棒的一个小方法.具体实现如下: - (void)webViewDidFinishLoad:(UIWebView *)webView1 { ...

  5. 通过改变uiview的layer的frame来实现进度条

    #import <UIKit/UIKit.h> @interface ProgressView : UIView @property(nonatomic,assign)CGFloat pr ...

  6. api 翻译之AsyncTask

    AsyncTask 类的简介: AsyncTask可以使UI线程更合理更简单的使用.这个类允许执行后台操作,而且可以在不使用多线程或handlers的情况下给主线程传输数据. 异步任务 被定义为在后台 ...

  7. Android读取RAM,ROM,SD卡容量

    1)简介 一般人们在买手机的时候,在手机配置上都会出现"内存容量:512MB ROM+512MB RAM "等等类似这样的说明,可能很多人都知道RAM的意思就是运存的意思,但是对于 ...

  8. 使你的 Google Summer of Code 建议被接收的5个技巧

    本文翻译自:http://www.di.ens.fr/~baghdadi/TXT_blog/5_advices_to_get_your_proposal_accepted.lyx.html 本文讲的主 ...

  9. CF Soldier and Badges (贪心)

    Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  10. codeforces 434B B. Nanami's Digital Board(分治)

    题目链接: B. Nanami's Digital Board time limit per test 1 second memory limit per test 256 megabytes inp ...