【Android进程保活】应⽤进程拉活(系统Service机制拉活Service组件onS。
。。
⽂章⽬录
⼀、 Service 组件 onStartCommand ⽅法分析
1、 onStartCommand 函数返回值分析
Service 的⽣命周期函数 onStartCommand ⽅法 , 返回⼀个整型值 ;
Service 中的 mStartCompatibility 标记默认是 fal , 因此 onStartCommand 函数默认返回的整型值是 Service.START_STICKY 值 ;
mStartCompatibility 值在 Service 中的 attach ⽅法中赋值 , 其值为 getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.ECLAIR ,即⼿机的 API Level 版本号是否⼩于 5 , 现在肯定没有版本号⼩于 5 的⼿机 , 该值默认是 fal ;
public abstract class Service extends ContextWrapper
implements ComponentCallbacks2,
ContentCaptureManager.ContentCaptureClient {
/**
* Called by the system every time a client explicitly starts the rvice by calling
* {@t.Context#startService}, providing the arguments it supplied and a * unique integer token reprenting 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 class="caution">Note that the system calls this on your
* rvice's main thread. A rvice'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 u {@link android.os.AsyncTask}.</p>
*
* @param intent The Intent supplied to {@t.Context#startService},
* as given. This may be null if the rvice 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.
* @param startId A unique integer reprenting this specific request to
* start. U with {@link #stopSelfResult(int)}.
*
* @return The return value indicates what mantics the system should
* u for the rvice's current started state. It may be one of the
* constants associated with the {@link #START_CONTINUATION_MASK} bits.
*
* @e #stopSelfResult(int)
*/
public@StartResult int onStartCommand(Intent intent,@StartArgFlags int flags,int startId){ onStart(intent, startId);
// mStartCompatibility 标记默认是 fal , 因此其返回值默认是 START_STICKY
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}同学用英语怎么说
/**
* @hide
琉璃砖*/
@UnsupportedAppUsage
public final void attach(
Context context,
ActivityThread thread, String className, IBinder token,
Application application, Object activityManager){
attachBaContext(context);
mThread = thread;// NOTE: unud - remove?
bubble ball
mClassName = className;
mToken = token;
mApplication = application;
mActivityManager =(IActivityManager)activityManager;
mStartCompatibility =getApplicationInfo().targetSdkVersion
< Build.VERSION_CODES.ECLAIR;
ContentCaptureOptions());
}
@UnsupportedAppUsage
废黜
private boolean mStartCompatibility =fal;
onStartCommand 的返回值有以下⼏种情况 :
Service.START_STICKY_COMPATIBILITY
Service.START_STICKY
Service.START_NOT_STICKY
Service.START_REDELIVER_INTENT
2、 onStartCommand 函数 START_STICKY_COMPATIBILITY 返回值
Service.START_STICKY_COMPATIBILITY : START_STICKY 兼容版本 , onStartCommand ⽅法返回该返回值时 , 不能保证服务可以重启 ;
/**
* 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;
3、 onStartCommand 函数 START_STICKY 返回值
Service.START_STICKY : onStartCommand ⽅法返回该 START_STICKY 返回值时 , 如果在执⾏ onStartCommand 后 , 如果Service 服务进程被杀掉 , 系统会保留 Service 状态 , 但是不保留启动服务的 Intent ; 之后系统会尝试重新创建该 Service 服务 ; ( 更详细的信息查看下⽅的源码注释 )sat考试
pep小学英语六年级下册教案/**
* Constant to return from {@link #onStartCommand}: if this rvice'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 rvice. Becau it is in the started state, it will
* guarantee to call {@link #onStartCommand} after creating the new
* rvice instance; if there are not any pending start commands to be
* delivered to the rvice, it will be called with a null intent
* object, so you must take care to check for this.
*
* <p>This mode makes n for things that will be explicitly started
* and stopped to run for arbitrary periods of time, such as a rvice
* performing background music playback.freshness
*/
public static final int START_STICKY =1;
4、 onStartCommand 函数 START_NOT_STICKY 返回值
Service.START_NOT_STICKY : " ⾮粘性 " , onStartCommand ⽅法返回该返回值时 , 如果在执⾏ onStartCommand 后 , 服务被杀死 ,系统不会重启 Service 服务 ;
/**
* Constant to return from {@link #onStartCommand}: if this rvice'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 rvice out of the started state and
* don't recreate until a future explicit call to
* {@link Context#startService Context.startService(Intent)}. The
* rvice will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent becau it will not be restarted if there
* are no pending Intents to deliver.
*
* <p>This mode makes n 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 themlves again later to do more work. An
* example of such a rvice would be one that polls for data from
* a rver: it could schedule an alarm to poll every N minutes by having
* the alarm start its rvice. 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 rvice will not be restarted until the
* alarm goes off.
*/
public static final int START_NOT_STICKY =2;
5、 onStartCommand 函数 START_REDELIVER_INTENT 返回值考研报考点能随便选吗
Service.START_REDELIVER_INTENT : 重传 Intent ; onStartCommand ⽅法返回该返回值时 , 如果在执⾏ onStartCommand 后 , 服务被杀死 , 系统会⾃动重启 , 并传⼊ Intent 值 , 不会传⼊ null ;
/**
* Constant to return from {@link #onStartCommand}: if this rvice'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 rvice calls {@link #stopSelf(int)} with the
oblong* start ID provided to {@link #onStartCommand}. The
* rvice will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent becau it will only be restarted if
* it is not finished processing all Intents nt to it (and any such
* pending events will be delivered at the point of restart).
*/
public static final int START_REDELIVER_INTENT =3;
⼆、系统 Service 机制拉活
根据上述 onStartCommand ⽅法返回值分析 , 只要返回值是 START_STICKY , 那么被杀掉的应⽤就会被重新拉起 ;
1、 Service 代码
package kim.hsl.keep_progress_alive.stick_rvice;
import android.app.Service;
t.Intent;
import android.os.IBinder;
public class StickService extends Service {
public StickService(){
}
@Override
public IBinder onBind(Intent intent){
return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){ StartCommand(intent, flags, startId);
}
}
kj是什么2、清单配置