2014-10-21 23:40 32 people read Comments (0) favorite reports
On the demo, start a service, execution order: onCreate-> Onstartcommand
Then close the app and re-execute the above two steps.
But copy the code to the game engineering discovery, after closing the game, only executed the onstart, but did not execute onstartcommand!
Find the following article:
[Plain]View Plaincopy
- Details of the Onstartcommand () method inside the service
- When the service is started, the OnCreate method is called only the first time, Onstartcommand and OnStart are called each time. Onstartcommand will tell the system how to restart the service, such as determining whether to restart after an abnormally terminated, and under what circumstances the abnormal termination
- Onstartcommand and OnStart differences
- This was the old OnStart method that would be called on the pre-2.0
- Platform. On 2.0 or later we override Onstartcommand () so this
- Method won't be called.
- After 2.0 API level, implementing onstart is equivalent to overriding Onstartcommand and returning Start_sticky
- @Override
- public void OnStart (Intent Intent, int startid) {
- Handlecommand (Intent);
- }
- After 2.0 API level, the OnStart () method is replaced by Onstartcommand ()
- @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;
- }
- Oncreate,onstartcommand,onstart is executed sequentially when the service is started, and if the service is restarted before the system shows calls to StopService and Stopself, the Onstartcommand is called. Onstartcommand,onstart is executed sequentially when the service is restarted. At any time, Onstartcommand () is called First, and OnStart () is called.
- Onstartcommand return value
- When Onstartcomand is used, it returns an (int) shaping.
- This shaping can have four return values: Start_sticky, Start_no_sticky, Start_redeliver_intent, start_sticky_compatibility.
- Their meanings are:
- 1): Start_sticky: If the service process is killed, the status of the reserved service is the start state, but the delivery intent object is not preserved. The system then tries to recreate the service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null.
- 2): Start_not_sticky: "Non-sticky". With this return value, if the service is aborted after executing Onstartcommand, the system does not automatically restart the service
- 3): start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed by an exception after Onstartcommand is executed, the service is automatically restarted and the value of intent is passed in.
- 4): Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be restarted after kill.
- Onstartcomand parameter flags meaning
- Flags indicates how the service is started:
- Additional data about this start request. Currently either 0, Start_flag_redelivery, or start_flag_retry.
- Start_flag_redelivery: If you implement Onstartcommand () to schedule asynchronous work or work in another thread, you may need to use Start_flag_redelivery to have the system resend a intent. This way, if your service is killed when it is processed, intent will not be lost.
- Start_flag_retry: Indicates that the service was set to Start_sticky before this token is passed in.
Then return start_redeliver_intent in the Onstartcommand function , solve the problem.
[Java]View Plaincopy
- @Override
- public int Onstartcommand (Intent Intent, int flags, int startid)
- {
- LOG.I ("CP","Push_service onstartcommand" +"flags=" +flags+"startid=" +startid+"packagename=" + Push_service. this.getpackagename ());
- M_sdcardpath=environment.getexternalstoragedirectory (). GetPath ();
- M_pushfiledirpath=m_sdcardpath+file.separator+push_service. This.getpackagename ();
- m_pushfilepath=m_pushfiledirpath+file.separator+"Push.txt";
- if (mmessagethread!=null)
- {
- Mmessagethread.mrunable=false;
- }
- mmessagethread=new Messagethread ();
- Mmessagethread.start ();
- //super.onstartcommand (Intent, flags, Startid);
- return start_redeliver_intent;
- }
Onstartcommand () method in the Android Service lifecycle service