安卓新手,笔记有理解不当的地方望指出,经过几天折腾终于可以实现类似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. js运动框架逐渐递进版

    运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓冲运动. 多物体运动 ...

  2. leetcode202

    public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...

  3. json小知识

    json转换新方法 上面是把一个json数组转换成map集合 fromObject()是通用的 map集合,bean对象,String转换成json 文件转换成字符串,在转换成json 生成json文 ...

  4. DEMO: springboot 与 freemarker 集成

    直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...

  5. Webservice 返回数据集 DataSet 及Android显示数据集LiveBindings

    一.服务端 New TSoapDataModule 添加控件 TDataSetProvider,TClientDataSet,TADOQuery,TADOConnection 添加方法 functio ...

  6. greenlet 实现手动协程切换

    from greenlet import greenlet def test1(): print('12') gr2.switch() #切换到gr2 print('34') gr2.switch() ...

  7. 根据img的url 判断img的图片大小

    // 图片地址 后面加时间戳是为了避免缓存 var img_url = 'http://www.qttc.net/static/upload/2013/13643608813441.jpg?'+Dat ...

  8. Java操作Excel之Poi

    package com.java1234.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSF ...

  9. C#实现支持单点登录的一个存储用户信息的类

    网上有很多介绍单点登录的文章,但多为架构设计以及概念性文章,而本文将介绍单点登录的具体具体实现 利用哈希表,作为保存登录用户的队列        private static Hashtable m_ ...

  10. php拓展安装

    Yaf安装配置:http://www.laruence.com/manual/yaf.install.html#yaf.installation.linux 下载Yaf的最新版本, 解压缩以后, 进入 ...