Android development-key points about Service

Source: Internet
Author: User

A service can be called in either of the following ways: startService and bindService ). The former requires the onStartCommand method in the Service class, and the Service needs to be stopped manually after it is started; otherwise, it will run forever; the latter requires the onBind method. When no component is bound to this Service, it is disconnected by itself.
The Service must be declared in the manifest file. You can use intent-filter to enable other programs to start this Service.
Note! The Service runs in the process where the current application is located. We recommend that you create a new thread in the Service to execute the task. Otherwise, the application may become stuck. Directly use IntentService to avoid this problem. It has even implemented a task queue to solve the concurrency problem. If concurrent functions are required, you can only inherit and implement the Service class on your own.
The returned values of onStartCommand () include START_NOT_STICKY, START_STICKY, and START_REDELIVER_INTENT. Click the corresponding link for details.
If you want the Service to return a result when starting the Service with startService, you can use PendingIntent as the parameter and use broadcast to obtain the return value.

BindService is used when we need to interact with the Service or want to expose some functions to other programs. The onBind method needs to be implemented in the Service and an IBinder interface is returned. We need to define some methods in this interface so that the components bound to the Service can interact with the Service through these methods. UnBindService is used for unbinding.
Service can use Toast Notifications or Status Bar Notifications to notify users of some information.

When a Service exists, such as a player, you can use startForeground () when starting the service (). The sample code is as follows:
[Java]
Notification notification = new Notification (R. drawable. icon, getText (R. string. ticker_text), System. currentTimeMillis (); <br/> Intent icationicationintent = new Intent (this, ExampleActivity. class );
PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, icationicationintent, 0 );
Notification. setLatestEventInfo (this, getText (R. string. icationication_title ),
GetText (R. string. icationication_message), pendingIntent );
StartForeground (ONGOING_NOTIFICATION, notification );


Service lifecycle:
[Java]
Public class ExampleService extends Service {
Int mStartMode; // how to behave if the service is killed
IBinder mBinder; // interface for clients that bind
Boolean mAllowRebind; // indicates whether onRebind shocould be used
 
@ Override
Public void onCreate (){
// The service is being created
}
@ Override
Public int onStartCommand (Intent intent, int flags, int startId ){
// The service is starting, due to a call to startService ()
Return mStartMode;
}
@ Override
Public IBinder onBind (Intent intent ){
// A client is binding to the service with bindService ()
Return mBinder;
}
@ Override
Public boolean onUnbind (Intent intent ){
// All clients have unbound with unbindService ()
Return mAllowRebind;
}
@ Override
Public void onRebind (Intent intent ){
// A client is binding to the service with bindService (),
// After onUnbind () has already been called
}
@ Override
Public void onDestroy (){
// The service is no longer used and is being destroyed
}
}

Unlike the Activity, we do not need to call the super implementation of these methods when rewriting.

Recently I used the Service, and it seems that the relationship is still quite messy. The self-implemented Service classes, interfaces, Listener and Binder are not thoroughly understood yet and need to be further familiarized.

 


From tangwing's column

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.