安卓定时通知的研究与实现思路
来源:互联网 发布:魔眼软件 编辑:程序博客网 时间:2024/04/30 13:43
安卓的推送一直是一个难题,当然你说简单我也不介意(使用第三方的是很简单)。
这里简单说明一下我这里的实现方案Service+reciver;
其中在service中创建一个进程并保持这个进程不会被系统和第三方清理工具杀死,
在这个进程中隔一段时间就去获取一下服务器的推送信息,
如果不是空,就创建一个Alarm通知reciver去发送通知到通知栏实现推送。
简易代码如下:
//reciver
public class ZLSReciver extends BroadcastReceiver { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) //使用Notification.Builder就需要api16的支持 @Override public void onReceive(Context context, Intent intent) { Log.d("DMX", "onReceive"); int intentID = intent.getIntExtra("id", -1); { String ticker = intent.getStringExtra("ticker"); String text = intent.getStringExtra("text"); String title = intent.getStringExtra("title"); if (ticker != null && text != null && title != null) { Intent intent2Main = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2Main, 0); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(context) .setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setTicker(ticker) .setContentText(text) .setContentTitle(title) .setWhen(System.currentTimeMillis()) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) .setLights(Color.BLUE, 0, 1) .setAutoCancel(true) .build(); nm.notify(intentID, notification);//发送通知 } }}}receiver的代码看起来并不是很难,只是收到信息就发送一个通知,但是service就并不简单了,这里需要维护一个进程且不会被回收,并同时reciver。下面来看看简化版的service代码。
public class PushService extends Service{ // 获取消息线程 private static MessageThread messageThread = null; //delete 通知 public void notificationDeleteAll() { NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.cancelAll(); } @Override public IBinder onBind(Intent arg0) { return null; } public void onCreate() { Log.d("PushService", "reg notification service start"); // 开启线程 messageThread = new MessageThread(); messageThread.start(); } @Override public int onStartCommand(final Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } class MessageThread extends Thread { // 设置是否循环推送 public boolean isRunning = true; public void run() { while (isRunning) { if(!isRunning) { break; } String message = getServermessage(); if(message != null) { //解析服务器数据并创建alarm发送信息给reciver } else { //这里维护进程不会被杀死,简单起见就以sleep代替,各位自由发挥吧 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } @Override public void onDestroy() { messageThread.isRunning = false; Log.d("PushService","destroy notification service"); super.onDestroy(); } public String getServermessage() { String res = "renult"; return res; }}
注意:以上代码只是为说明思路和逻辑的简易代码,只供参考,不可放在项目里面直接运行
0 0
- 安卓定时通知的研究与实现思路
- 安卓开发:通知栏通知的实现
- Android应用的定时通知实现
- 实现定时和调度的思路
- Android 实现一些定时计数的思路
- 安卓实现clean architecture的大体思路
- iOS - scrollView与headerView的视差滚动实现思路 scrollView 可接收通知啊
- 安卓的通知组件
- 安卓之实现activity间的定时跳转
- 安卓App的备份与恢复-不定时更新
- 旧日本海军建军思路与用兵思路的研究
- 安卓通知与NotificationCompat.Builder
- 【安卓】安卓App开发思路 一步一个脚印(二)FragmentTabHost实现底部的菜单
- 【安卓】安卓App开发思路 一步一个脚印(十五)实现闪屏的优化
- 关于安卓跑马灯效果实现的课题研究
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(七)实现ViewPager无限循环与自动播放
- nagios实现定时发送通知
- 安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知
- 常用插件
- nginx、fastCGI、php-fpm关系梳理
- NetRiver - IPv6协议转发实验
- 十一章 上机练习4 + 5
- Android 解决使用Log打印日志的时候中文是乱码(unicode)
- 安卓定时通知的研究与实现思路
- Assign Cookies(分蛋糕)
- 深度学习应用
- oracle数据库导入导出之imp,exp,impdp,expdp等工具的使用
- 在ubuntu16.04下载&编译android源代码
- socket 基本套接字1
- C# 第10章
- 2016.11.25笔记
- javascript学习笔记(2)