android Service的stopSelf
来源:互联网 发布:标签设计软件 编辑:程序博客网 时间:2024/06/12 01:28
startId:代表启动服务的次数,由系统生成。
stopSelf(int startId):
在其参数startId跟最后启动该service时生成的ID相等时才会执行停止服务。
stopSelf():直接停止服务。
使用场景:
如果同时有多个服务启动请求发送到onStartCommand(),不应该在处理完一个请求后调用stopSelf();因为在调用此函数销毁service之前,可能service又接收到新的启动请求,如果此时service被销毁,新的请求将得不到处理。此情况应该调用stopSelf(int startId)。
而且stopSelf是会调用Destroy方法的。
需要注意的是,如果是前台服务,你就必须要调到startForeground和stopForeground。如果仅仅是一个Notifaction,设置的flags是FLAG_FOREGROUND_SERVICE,这样是没有意义的,并且调用cancel方法是无法取消这个通知的,这个时候可以设置成FLAG_ONGOING_EVENT
public int onStartCommand(Intent intent, int flags, int startId) { PendingIntent notifIntent; Intent notificationIntent = new Intent(); // fill up yourself notifIntent = PendingIntent.getActivity(); // fill up yourself Notification note = new Notification(); // fill up yourself note.setLatestEventInfo(context, getString(R.string.app_name), Message, notifIntent); startForeground(yourOwnNumber, note); // fill up yourself displayYourNotification(); // Fill up the rest yourself.}
public void onDestroy() { stopForeground(); // fill up yourself}public void displayYourNotification() { Intent notificationIntent = new Intent(); // fill up yourself notifIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Notification note = new Notification(); // fill up yourself note.flags = Notification.FLAG_FOREGROUND_SERVICE; note.setLatestEventInfo(context, TITLE, Message, notifIntent); notifManager.notify(ID, note);}
0 0
- android Service的stopSelf
- android Service的stopself方法
- android Service stopSelf(int startId)与stopSelf()的区别
- android Service stopSelf(int startId)与stopSelf()的区别
- Android Service stopself方法
- #android# service stopself() stopservice()
- 停止Service的stopSelf(int)
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- 使用Service的stopSelf()方法来停止服务
- stopSelf()方法的详解
- Service中stopSelf(int startId)说明
- stopSelf()方法的使用注意
- 关于StopSelf
- stopSelf() vs stopSelf(int) vs stopService(Intent)
- Android的Service概括
- Android Service的使用
- Android Service 的使用
- Android service的使用
- 8、一个单链表,不给头指针,只给出当前结点,删除该结点。
- 此代码可以登录GOOGLE
- hdu 4941 Magical Forest
- poj 3278 Catch That Cow (bfs搜索)
- Jstl标签<c:forEach>的用法
- android Service的stopSelf
- STL中各容器比较
- 描述Cookie和Session的作用,区别和各自的应用范围,cookie、Session工作原理
- Myeclipse10设置优化
- php 对象
- 剑指offer--面试题49:把字符串转换成整数
- leetcode 刷题之路 86 Balanced Binary Tree
- Jstl标签<c:if>的用法
- hjnrahashvfypp dxdisdfdsfyqrfcxwf