Android Service Notes

Source: Internet
Author: User

1, service by default in the main thread run, so can not be directly in the service to access the network, operation files and other time-consuming operations, to open the thread separately

2, through the StartService Open service, once the service opened, this service and open his callers there is no relationship between the caller can not access the service inside the method, the caller if the system is recycled or called the OnDestroy method, Service will also continue to exist

3, through the Bindservice Open service, after the service opened, there is still a connection between the caller and the service, once the caller has hung up, the service will be followed by hanging off

4. Toast can be used in service

5. Intentservice is a service that comes with a thread, can shut itself down, and is typically used for single-threaded work tasks that are automatically closed after completion

6. Data interaction between service and activity three ways:

First, if the activity invokes the service's method to get the return value, then when the activity wants to get information from the service, the method in the service is called. However, this approach has limitations because the activity does not know when the information in the service is updated and cannot passively receive information.

Second, using the broadcast receiver can solve the limitations of Method 1, when the information in the service is updated to send a broadcast to the activity, the activity registers a broadcast receiver to receive the broadcast, in order to update the information in the activity. However, there is a question, assuming that the frequent transmission of the broadcast will not consume large resources.

Three, write a callback interface
The steps are as follows:
1. First write an interface to do the callback
Public interface ICount {
void count (int val);
}
2.Service class
public class Countservice extends Service {
private int val = 0;

public void Startcount (ICount ICount) {//Call countactivity, because countactivity implements ICount
Do something ...
Val + +;
Icount.count (Val);
}
}
3.Activity class
Note: The code that binds the Countservice is omitted
public class Countactivity extends Activity implements ICount {
@Override
protected void OnCreate () {
Do something ...
Startcount (this); Call the Startcount () method of the Countservice object and pass it in so that the Countservice object can invoke its own method.
}

@Override
void count (int val) {
Update UI
}
}

7. Binder can send data from activity to service by calling Transact, and return data from service

8. Service sticky and non-sticky

Start service is also classified as sticky and non-sticky:
(1) Sticky service: Can not be shut off, due to memory tension will be revived after the close
(2) Non-sticky service: dead, dead.
The difference between viscous and non-viscous is: Look at the return value of Onstartcommand.
If the system crashes before returning, there is no stickiness or non-stickiness.
Non-sticky: The return value is Start_not_sticky system kill, restart need to start StartService
Stickiness: The return value Start_sticky after the service has died, but is not called Startserivce, but instead calls the service's Startcommand method directly, all the intent that pass over are null.
Return value start_redeliver_intent: After the death of the system itself, but still will pass the last INTENT (because the last cause of death, so the INTENT may still die, but died four or five times will not automatically start. )

9. Communication between processes can be made through Messager

10, Aidl Demo:http://git.oschina.net/atwal/androidaidldemo

Android Service Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.