【移动开发】Service类onStartCommand()返回值和参数
从Android官方文档中,知道onStartCommand有4种返回值:
http://developer.android.com/reference/android/app/Service.html
/**
* Constant to return from {@link #onStartCommand}: compatibility
* version of {@link #START_STICKY} that does not guarantee that
* {@link #onStartCommand} will be called again after being killed.
*/
public static final int START_STICKY_COMPATIBILITY = 0; /**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then leave it in the started state but
* don't retain this delivered intent. Later the system will try to
* re-create the service. Because it is in the started state, it will
* guarantee to call {@link #onStartCommand} after creating the new
* service instance; if there are not any pending start commands to be
* delivered to the service, it will be called with a null intent
* object, so you must take care to check for this.
*
* <p>This mode makes sense for things that will be explicitly started
* and stopped to run for arbitrary periods of time, such as a service
* performing background music playback.
*/
public static final int START_STICKY = 1; /**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), and there are no new start intents to
* deliver to it, then take the service out of the started state and
* don't recreate until a future explicit call to
* {@link Context#startService Context.startService(Intent)}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will not be re-started if there
* are no pending Intents to deliver.
*
* <p>This mode makes sense for things that want to do some work as a
* result of being started, but can be stopped when under memory pressure
* and will explicit start themselves again later to do more work. An
* example of such a service would be one that polls for data from
* a server: it could schedule an alarm to poll every N minutes by having
* the alarm start its service. When its {@link #onStartCommand} is
* called from the alarm, it schedules a new alarm for N minutes later,
* and spawns a thread to do its networking. If its process is killed
* while doing that check, the service will not be restarted until the
* alarm goes off.
*/
public static final int START_NOT_STICKY = 2; /**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then it will be scheduled for a restart
* and the last delivered Intent re-delivered to it again via
* {@link #onStartCommand}. This Intent will remain scheduled for
* redelivery until the service calls {@link #stopSelf(int)} with the
* start ID provided to {@link #onStartCommand}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will will only be re-started if
* it is not finished processing all Intents sent to it (and any such
* pending events will be delivered at the point of restart).
*/
public static final int START_REDELIVER_INTENT = 3;
参数
onStartCommand()的函数原型,代码如下:
/**
* Called by the system every time a client explicitly starts the service by calling
* {@link android.content.Context#startService}, providing the arguments it supplied and a
* unique integer token representing the start request. Do not call this method directly.
*
* <p>For backwards compatibility, the default implementation calls
* {@link #onStart} and returns either {@link #START_STICKY}
* or {@link #START_STICKY_COMPATIBILITY}.
*
* <p>If you need your application to run on platform versions prior to API
* level 5, you can use the following model to handle the older {@link #onStart}
* callback in that case. The <code>handleCommand</code> method is implemented by
* you as appropriate:
*
* {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
* start_compatibility}
*
* <p class="caution">Note that the system calls this on your
* service's main thread. A service's main thread is the same
* thread where UI operations take place for Activities running in the
* same process. You should always avoid stalling the main
* thread's event loop. When doing long-running operations,
* network calls, or heavy disk I/O, you should kick off a new
* thread, or use {@link android.os.AsyncTask}.</p>
*
* @param intent The Intent supplied to {@link android.content.Context#startService},
* as given. This may be null if the service is being restarted after
* its process has gone away, and it had previously returned anything
* except {@link #START_STICKY_COMPATIBILITY}.
* @param flags Additional data about this start request. Currently either
* 0, {@link #START_FLAG_REDELIVERY}, or {@link #START_FLAG_RETRY}.
* @param startId A unique integer representing this specific request to
* start. Use with {@link #stopSelfResult(int)}.
*
* @return The return value indicates what semantics the system should
* use for the service's current started state. It may be one of the
* constants associated with the {@link #START_CONTINUATION_MASK} bits.
*
* @see #stopSelfResult(int)
*/
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
官方解释如下:
public int onStartCommand (Intent intent, int flags, int startId)
Called by the system every time a client explicitly starts the service by calling startService(Intent)
, providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
For backwards compatibility, the default implementation calls onStart(Intent, int)
and returns either START_STICKY
or START_STICKY_COMPATIBILITY
.
If you need your application to run on platform versions prior to API level 5, you can use the following model to handle the older onStart(Intent, int)
callback in that case. The handleCommand
method is implemented by you as appropriate:
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls, or heavy disk I/O, you should kick off a new thread, or use AsyncTask
.
Parameters
intent | The Intent supplied to startService(Intent) , as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY . |
---|---|
flags | Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY , or START_FLAG_RETRY . |
startId | A unique integer representing this specific request to start. Use with stopSelfResult(int) . |
Returns
- The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the
START_CONTINUATION_MASK
bits.
See Also
flags有3种取值,flags值和onStartCommand()的返回值有着直接的关系。如果Service被系统意外终止,重启的时候,传入的flags。官方文档描述:
public static final int START_FLAG_REDELIVERY
This flag is set in onStartCommand(Intent, int, int)
if the Intent is a re-delivery of a previously delivered intent, because the service had previously returned START_REDELIVER_INTENT
but had been killed before calling stopSelf(int)
for that Intent.
public static final int START_FLAG_RETRY
This flag is set in onStartCommand(Intent, int, int)
if the Intent is a a retry because the original attempt never got to or returned from onStartCommand(Intent, int, int)
.
【移动开发】Service类onStartCommand()返回值和参数的更多相关文章
- Service#onStartCommand返回值解析
Service#onStartCommand返回值解析 Service类有个生命周期方法叫onStartCommand,每次启动服务(startService)都会回调此方法.此方法的原型例如以下: ...
- C++利用不完全实例化来获得函数模板参数的返回值和参数
有一些模板会以函数为模板参数,有时候这些模板要获得函数的返回值和参数.如在boost中的signal和slot机制,就存在这样情况. 那么,我们如何得到这些信息呢? 我们使用C++不完全实例化来实现. ...
- C#调用存储过程详解(带返回值、参数输入输出等)
CREATE PROCEDURE [dbo].[GetNameById] @studentid varchar(8), @studentname nvarchar(50) OUTPUT AS BEGI ...
- Android中Service类onStartCommand的返回值问题
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...
- Android中Service类onStartCommand
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...
- Android平台调用Web Service:线程返回值
接上文 前文中的遗留问题 对于Java多线程的理解,我曾经只局限于实现Runnable接口或者继承Thread类.然后重写run()方法.最后start()调用就算完事,可是一旦涉及死锁以及对共享资源 ...
- java.lang.String类compareTo()返回值解析
一.compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...
- python开发初识函数:函数定义,返回值,参数
一,函数的定义 1,函数mylen叫做函数名 #函数名 #必须由字母下划线数字组成,不能是关键字,不能是数字开头 #函数名还是要有一定的意义能够简单说明函数的功能 2,def是关键字 (define) ...
- Process类调用exe,返回值以及参数空格问题
(方法一)返回值为int fileName为调用的exe路径,入口参数为para,其中多个参数用空格分开,当D:/DD.exe返回值为int类型时. Process p = new Process() ...
随机推荐
- ●POJ 3237 Tree
题链: http://poj.org/problem?id=3237 题解: LCT 说一说如何完成询问操作就好了(把一条链的边权变成相反数的操作可以类比着来): 首先明确一下,我们把边权下放到点上. ...
- 2015 多校联赛 ——HDU5316(线段树)
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...
- hdu 5492 (暴力+nice)
题意:在矩阵中,找一条路从 (1,1)->(n,m),使方差最小 思路: T = (N+M−1)∑N+M−1i=1(Ai−Aavg)2 将N + M - 1乘进去,即求1 ~ N+M-1,(N ...
- 【吃炸弹的鸽子UVA10765-双联通模板】
·从前有一个鸽子Lence,它吃了一个炸弹,然后有人出了这道题. ·英文题,述大意: 给出一张连通无向图,求出:对于每个点,删去这个点(以及它相连的边以后)时,当前图中的连通块数量,这个 ...
- poj2947 高斯消元
Widget Factory Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 5218 Accepted: 1802 De ...
- bzoj2237[NCPC2009]Flight Planning 结论题?
2237: [NCPC2009]Flight Planning Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 55 Solved: 27[Submi ...
- k-d树模板(BZOJ2648)
实现了插入一个点,查询距某个位置的最近点. #include <cstdio> #include <algorithm> using namespace std; , inf ...
- FFT模板(BZOJ2179)
实现了两个长度为n的大数相乘. #include <cstdio> #include <cmath> #include <complex> using namesp ...
- TensorFlow Serving-TensorFlow 服务
TensorFlow服务是一个用于服务机器学习模型的开源软件库.它处理机器学习的推断方面,在培训和管理他们的生命周期后采取模型,通过高性能,引用计数的查找表为客户端提供版本化访问. 可以同时提供多个模 ...
- jquery 跨域请求数据问题
昨天参加了一个前端的面试,被问到一个跨域请求数据问题,我们之前一直用的是apicloud的api进行请求的,跨域是被apicloud封装起来的,也就没有注意跨域请求数据的问题.当被问到用jquery跨 ...