android 后台服务、通知信息
2016-08-03 20:07
507 查看
IntentService 也是 Context,需要在 Manifest 配置中添加<service android:name=".abcservice"/>
创建 abcService extends IntentService,重载 onHandleIntent 后就可以用 startService(Intent) 启动 abcService 了。
因为 Service 具有后台运行的特性,我们可以使系统不断运行它以达到检测消息等目的。在abcService 中添加方法:
将 startService 修改为 abcService.setServiceAlarm(getActivity(),true); 这样即可以15秒为间隔反复运行abcService。
分析代码:注意
PendingIntent,getService 等价于 将来 startService(Intent),getActivity 等价于将来 startActivity,同理还有 getBroadcast。
后台服务常常会发送通知给用户 ,比如收到了QQ消息。在onHandleIntent 适当处给用户发送通知:
Resources r = getResources();
PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
Notification n = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.n_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(z.getString(R.string.n_title))
.setContentText(r.getString(R.string.n_text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManagero)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,n);setAutoCancel(true) 表明当用户点击此 Notification 后自动将该消息删除。注意 notify 第一个参数id 0,当再次发送通知时,相同id的通知会被替换。
参考
Notification使用以及PendingIntent.getActivity()
创建 abcService extends IntentService,重载 onHandleIntent 后就可以用 startService(Intent) 启动 abcService 了。
因为 Service 具有后台运行的特性,我们可以使系统不断运行它以达到检测消息等目的。在abcService 中添加方法:
public static void setServiceAlarm(Context c, boolean on){ AlarmManager am = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getService(c,0,new Intent(c,abcService.class),0); if(on){am.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),1000*15,pi);} else{am.cancel(pi);pi.cancel();} } public static boolean isServiceAlarmOn(Context c){ PendingIntent pi = PendingIntent.getService(c,0,new Intent(c,abcService.class),PendingIntent.FLAG_NO_CREATE); return pi!=null; }
将 startService 修改为 abcService.setServiceAlarm(getActivity(),true); 这样即可以15秒为间隔反复运行abcService。
分析代码:注意
PendingIntent,getService 等价于 将来 startService(Intent),getActivity 等价于将来 startActivity,同理还有 getBroadcast。
后台服务常常会发送通知给用户 ,比如收到了QQ消息。在onHandleIntent 适当处给用户发送通知:
Resources r = getResources();
PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
Notification n = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.n_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(z.getString(R.string.n_title))
.setContentText(r.getString(R.string.n_text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManagero)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,n);setAutoCancel(true) 表明当用户点击此 Notification 后自动将该消息删除。注意 notify 第一个参数id 0,当再次发送通知时,相同id的通知会被替换。
参考
Notification使用以及PendingIntent.getActivity()
相关文章推荐
- android后台服务service全解析(中)--IntentService与Notification前台通知
- Android程序后台开启服务,显示通知栏
- android双后台服务,消息通知类
- android双后台服务,消息通知类(service1)
- android 后台服务定时通知
- android双后台服务,消息通知类(service2)
- android客户端通过无什么与后台服务信息传输??
- Android 如何屏蔽联通服务信息WAP通知信息http://g.iuni.com.cn, 百度无耻不让发贴批拼联通
- Android中后台服务与通知的协作工作
- android 双后台服务,消息通知 (mainActivity)
- Android 软件升级(后台服务 + 通知栏进度条)
- Android Notification 使用详解之四:由后台服务向Activity发送进度信息
- Android系统的进程,任务,服务的信息
- 枚举Android系统的进程、任务和服务的信息
- 会发送通知信息的Android手机病毒
- android本地后台服务示例
- Android 应用五大核心块 --- Service后台服务
- 枚举Android系统的进程,任务和服务的信息
- 多源在线与离线数据读取之五(Android移动端开发读取四川地理信息中心地名搜索服务)
- 由后台服务向Activity发送进度信息