AppStartA log is opened in which a service LogUploadService is used to upload applications.
Use the start way to open the service, the code is as follows:
new Intent(this, LogUploadService.class);startService(uploadLog);
First, Function Introduction:
After the service LogUploadService is turned on, the following actions are performed as appropriate:
- Read the log information under the OSC local folder
- If the log information is empty, the service stops--
LogUploadService.this.stopSelf()
- If the log information is not empty, upload the log;
Ii. details of the cessation service
In this service operation, no matter what circumstances need to stop the service (log information is empty, log upload success, log upload failed), stop the service, use the following code:
LogUploadService.this.stopSelf()
About Stopself, as described in Google's official documentation:
If A component starts the service by calling StartService () (which results in a call to Onstartcommand ()) and then the Servic E remains running until it stops itself with stopself () or another component stops it by calling StopService ().
If a component is turned on by StartService () to open the service (actually called the Onstartcommand ()) method, then the service has been running to know that it called stopself () stopped itself or another component by calling stopService method to stop the service.
Onstartcommand ()
The system calls this method when another component, such as a activity, requests that the service is started, by calling StartService (). Once This method executes, the service was started and can run in the background indefinitely. If you implement this, it's your responsibility to stop the service when it's work was done, by calling Stopself () or StopS Ervice (). (If you have want to provide binding, you don't need to implement this method.)
The system calls the Onstartcommand () method when a component, such as activity, invokes the StartService () method to open a service. Once this method is executed, the service will be opened and run independently in the background. If you implement this method, you must call stopSelf() or stop the service after the task is completed stopService() . (If you just want to provide bindings, you don't need to implement this method)
Upload Log
Log upload action, encapsulated in the Oschinaapi, with android-async-http to complete the operation, the code is as follows
"'
Oschinaapi.uploadlog (data, new Asynchttpresponsehandler () {
@Override
public void onsuccess (int arg0, header[] arg1, byte[] arg2) {
Upload succeeded, delete log of local record, and stop service
Log.delete ();
LogUploadService.this.stopSelf ();
}
@Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { //上传失败停止服务 LogUploadService.this.stopSelf(); } });
"'
This piece of encapsulation is very ingenious, in the OSCHINAAPI, the following encapsulation is carried out:
Upload into: Upload log uploadLog and upload feedback, according to the report feedback to distinguish the category of upload
"'
private static void Uploadlog (string data, String report,
Asynchttpresponsehandler handler) {
Requestparams params = new Requestparams ();
Params.put ("App", "1");
Params.put ("Report", report);
Params.put ("MSG", data);
Apihttpclient.post ("Action/api/user_report_to_admin", params, handler);
}
/**
* Bug Escalation
*
* @param data
* @param Handler
*/
public static void Uploadlog (String data, Asynchttpresponsehandler handler) {
Uploadlog (Data, "1", handler);
}
/** * 反馈意见 * * @param data * @param handler */public static void feedback(String data, AsyncHttpResponseHandler handler) { uploadLog(data, "2", handler);}
"'
Iii. Summary of the Harvest
Start mode to start the service, call the Onstartcommand method, and stop the service--stopself or StopService after the task ends
Upload a log and feedback code package Oschinaapi.java
Use of Android-async-http
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Open source Chinese source Learning (iii)--log log upload