安卓新手,笔记有理解不当的地方望指出,经过几天折腾终于可以实现类似ios的本地通知功能(ios就几行代码),可能有第三方sdk可以方便实现,暂时没去找

思路:

1。 startService 和bindService   同时存在

startService 新开进程 注意AndroidMenifest.xml中添加process

  

 <service android:name="org.cocos2dx.javascript.LocalPush"
android:process=":remote">
</service>

这里的remote可以自定义任意名称,前面冒号代表与原Activity不同的独立进程

因为两个进程互相独立,需要通讯,使用AIDL方式

Activity.java

 @Override

    protected void onStart() {  

    Log.i(TAG,"onStart");

        super.onStart();  

        Bundle args = new Bundle();  

        args.putString("param", "oper1");  

        Intent intent = new Intent(AppActivity.this, LocalPush.class);  

        intent.putExtras(args);  

        startService(intent); 

        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);   

    }  

    @Override

    protected void onStop() {  

    Log.i(TAG,"onStop");

        super.onStop();  

        unbindService(mConnection);  

    }   

 static forService mService;   

private forActivity mCallback = new forActivity.Stub() {
@SuppressLint("ShowToast") public void performAction() throws RemoteException {
Toast.makeText(AppActivity.this, "this toast is called from service", ).show();
}
};
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(TAG,"onServiceConnected");
mService = forService.Stub.asInterface(service);
try {
mService.registerTestCall(mCallback);
}
catch (RemoteException e) { }
}
public void onServiceDisconnected(ComponentName className) {
Log.i(TAG,"disconnect service");
mService = null;
}
};

LocalPush.java   继承自 Service

public class LocalPush extends Service {
private forActivity callback; // public LocalPush() {
// super("LocalPush");
// }
//
// @Override
// protected void onHandleIntent(Intent intent) {
// String action = intent.getExtras().getString("param");
// Log.i(TAG, "onHandleIntent");
// notifyArr = new Vector<CustomNotify>();
// messageNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// // IntentService会使用单独的线程来执行该方法的代码
// while (isRunning) {
// Log.i(TAG, "Service tick--->1");
// try {
// Log.i(TAG, "Service tick--->2");
// //休息1秒
// Thread.sleep(1000);
// Log.i(TAG, "Service tick--->3");
// sendNotifies();
// Log.i(TAG, "Service tick--->4");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// private final IBinder binder = new LocalBinder();
// public class LocalBinder extends Binder {
// LocalPush getService() {
// return LocalPush.this;
// }
// } private final forService.Stub mBinder = new forService.Stub() {
@Override
public void invokCallBack(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) throws RemoteException {
//callback.performAction();
registerLocalNotification(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
}
@Override
public void registerTestCall(forActivity cb) throws RemoteException {
callback = cb;
}
}; //当使用startService()方法启动Service时
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "IBinder onBin");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,"service on unbind");
return super.onUnbind(intent);
}
public void onRebind(Intent intent) {
Log.i(TAG, "service on rebind");
super.onRebind(intent);
} //Service时被调用
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate--->1");
super.onCreate();
//startForeground(-1213, new Notification());
Notification notification = new Notification();
notification.icon = R.drawable.icon; //通知图片 Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(LocalPush.this, AppActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(LocalPush.this, , intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(LocalPush.this, "Slots", "Click return to Slots!", pendingIntent);
startForeground(0x1982, notification); // notification ID: 0x1982, you can name it as you will.       messageThread = new MessageThread();       messageThread.start(); }
  class MessageThread extends Thread {     //运行状态     public boolean isRunning = true;     @Override     public void run() {       while(isRunning) {         synchronized(this) {         try {         //休息1秒           Thread.sleep();           sendNotifies(); //这里判断是否需要发出通知         } catch (Exception e) {           e.printStackTrace();           Thread.currentThread().interrupt();         }       }     }   } } @Override
//当调用者使用startService()方法启动Service时,该方法被调用
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStart--->1:"+ flags+" stargId:" + startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
//当Service不在使用时调用
public void onDestroy() {
Log.i(TAG, "Service onDestroy--->");
super.onDestroy();
stopForeground(true);
}
}

forActivity.aidl

package org.cocos2dx.javascript;
interface forActivity {
void performAction();
}

forService.aidl

package org.cocos2dx.javascript;
import org.cocos2dx.javascript.forActivity;
interface forService {
void registerTestCall(forActivity cb);
void invokCallBack(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean);
}

2 通知

cocos3 以上支持js反射到java,Activity中创建对应Static 方法 ,

大概这样

  public static void registerLocalNotification(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) {
Log.i(TAG, "registerLocalNotification1 "+_alertTime+" "+_key+" "+_alertBody+" "+_title+" "+_content+" "+_isEndClean);
//mService.registerLocalNotification(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
Log.i(TAG, "registerLocalNotification2 "+_alertTime+" "+_key+" "+_alertBody+" "+_title+" "+_content+" "+_isEndClean);
try {
mService.invokCallBack(_alertTime, _key, _alertBody, _title, _content, _isEndClean);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

service端相应方法,思路:开启新线程实时   比对通知世间簇和当前世间簇,如果结果为   小于等于   则发出通知

 //注册通知
public void registerLocalNotification(int _alertTime, String _key, String _alertBody, String _title, String _content, boolean _isEndClean) {
...
}

发通知大概这样


private Intent messageIntent = null;

private PendingIntent messagePendingIntent = null;

//通知栏消息

private int messageNotificationID = 1000;

private Notification messageNotification = null;

private NotificationManager messageNotificationManager;

@SuppressWarnings("deprecation")
public void commitNotify () {
finishFlag = true;
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon; //通知图片
messageNotification.defaults = Notification.DEFAULT_SOUND;
//点击查看 messageIntent = new Intent(Intent.ACTION_MAIN);
messageIntent.addCategory(Intent.CATEGORY_LAUNCHER);
messageIntent.setClass(LocalPush.this, AppActivity.class);
messageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // //Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED //messageIntent = new Intent(LocalPush.this, AppActivity.class);
messagePendingIntent = PendingIntent.getActivity(LocalPush.this, , messageIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//设置消息内容和标题
messageNotification.setLatestEventInfo(LocalPush.this, title, content, messagePendingIntent);
//messageNotification.when = System.currentTimeMillis();
messageNotification.number = ;
//messageNotification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示 //避免覆盖消息,采取ID自增
messageNotificationManager.notify(++ messageNotificationID, messageNotification);
}
												

cocos2d-js 安卓自定义本地通知功能的更多相关文章

  1. cocos2d JS 利用定时器实现-倒计时功能

    //创建一个定时器 cc.director.getScheduler().schedule(this, this.updates, 1, cc.REPEAT_FOREVER, 0, false, &q ...

  2. iOS: 本地通知的前后变化(iOS10)

    一.介绍  通知和推送是应用程序中很重要的组成部分.本地通知可以为应用程序注册一些定时任务,例如闹钟.定时提醒等.远程推送则更强大,提供了一种通过服务端主动推送消息到客户端的方式,服务端可以更加灵活地 ...

  3. UILocalNotification本地通知的使用方法

    本文所写方法主要应用UILocalNotification达到本地推送通知栏信息 取消了其他教程里过期的UIAlertView方法 使用UILocalNotification主要分为创建 调用 取消 ...

  4. 利用JS实现自定义滚动条

    一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...

  5. 在Unity3D中实现安卓平台的本地通知推送

    [前言] 对于手游来说,什么时候需要推送呢?玩过一些带体力限制的游戏就会发现,我的体力在恢复满后,手机会收到一个通知告诉我体力已完全恢复了.这类通知通常是由本地的客户端发起的,没有经过服务端. 在安卓 ...

  6. Ios开发中UILocalNotification实现本地通知实现提醒功能

    这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...

  7. Zabbix的通知功能以及自定义脚本告警

    本节内容: Zabbix的通知功能 定义接收告警的用户 定义Action Zabbix自定义脚本发送报警邮件 一.Zabbix的通知功能 在配置好监控项和触发器之后,一旦正常工作中的某触发器状态发生改 ...

  8. iOS开发中UILocalNotification本地通知实现简单的提醒功能

    这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...

  9. php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义

    https://blog.csdn.net/weixin_42231483/article/details/81585322 最近用PHP的tp3.2.3框架和js写的微信分享功能心得,分享的标题内容 ...

随机推荐

  1. uva-193-图染色-枚举

    题意:n个节点,可用描成黑色或者白色,黑节点和黑节点不能相连,问最多描出多少黑节点 #include <iostream> #include <stdio.h> #includ ...

  2. 温故而知新-PHP文件操作函数

    1 文件操作流程 打开文件->读取或者写入文件->关闭文件 fopen->fread,fwrite->fclose fopen可以打开ftp或者http协议的文件,前提示对方支 ...

  3. HTML5 位运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was implicitly closed

    网站运行一个晚上,早上来上班,发现报错: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTra ...

  5. Mysql 获取当天,昨天,本周,本月,上周,上月的起始时间

    转自: http://www.cppblog.com/tx7do/archive/2017/07/19/215119.html -- 今天 SELECT DATE_FORMAT(NOW(),'%Y-% ...

  6. 多媒体基础知识之PCM数据

    1.什么是PCM音频数据 PCM(Pulse Code Modulation)也被称为脉冲编码调制.PCM音频数据是未经压缩的音频采样数据裸流,它是由模拟信号经过采样.量化.编码转换成的标准的数字音频 ...

  7. python HttpServer共享文件

    在目录下运行 python -m SimpleHTTPServer python -m http.server 启动服务器.

  8. spring coud feign

    1. 依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...

  9. ML_入门

    N-gram 输入法后来提醒nlp自然语言理解一个向量映射到另一个空间,为什么是向量呢?模型其实是向量,一张图片表示成向量,像素表示成rgb ,每一个维度 数的度文本变成向量 one-hot repr ...

  10. 专业英语词汇(Java)

    abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]‘(n.入口, ...