The way we started the service in the previous article was:
StartService ()
New Intent ("Android.startservice"); // Differentiate service Types Bundle Bundle new bundle (); Bundle.putint ("Op", op); Intent.putextras (bundle); StartService (intent); // Intent.setclass can also be used here (This,startmusicservice.class); Such intent=new Intent ();
Bindservice
New Intent ("Android.bindservice"); // can also: Intent Intent = new Intent (mainactivity.this,bindmusicservice.class); // Context.bind_auto_create: The service is created automatically as long as the activity and service are bound Bindservice (Intent, SC, context.bind_auto_create);
Are methods that directly start the service. We can also start the Service (service) indirectly through activity and broadcast (broadcast), and this article will learn how to start the services by broadcasting broadcast.
Perhaps you would say that you can start with activity, why use the radio?
For example, the playback, pause, and stop of your music player are usually placed in a service. We had a phone call when we listened to the song, or we pulled out the headphones, and we asked to pause the music. How is this effect done? Call and unplug the headset the system sends a system message to the program. We can build a broadcastreceiver subclass, receive the message after registering the broadcast receiver, and then start the service in OnReceive (pause processing). If we start the service directly, we will not be able to start the service for the message (must be broadcast).
General: The Service is initiated indirectly by means of a broadcast: the processing is done for the system message, and this processing is in service.
Example:
Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <ButtonAndroid:id= "@+id/send"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start service via broadcast" /></LinearLayout>
Mainactivity.java
PackageCom.genwoxue.broadcastservice;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.IntentFilter; Public classMainactivityextendsActivity {PrivateBroadcastreceiverutil util=NULL; PrivateButton btnsend=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnsend= (Button)Super. Findviewbyid (R.id.send); Btnsend.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Send broadcast: Its action is "Com.genwoxue.action.ABOUTSERVICE"Intent intent=NewIntent ("Com.genwoxue.action.ABOUTSERVICE"); Mainactivity. This. Sendbroadcast (Intent); //instantiate a broadcast filter (only filter its action to "Com.genwoxue.action.ABOUTSERVICE")Intentfilter filter=NewIntentfilter ("Com.genwoxue.action.ABOUTSERVICE"); //instantiate a broadcast sink (receiver)Util=NewBroadcastreceiverutil (); //Register Broadcastreceiver: parameter is receiver and filterMainactivity. This. Registerreceiver (util, filter); } }); } @Overrideprotected voidOnStop () {//Stop Broadcasting Super. Unregisterreceiver (util); Super. OnStop (); //Stop ServiceIntent intent=NewIntent (mainactivity. This, Serviceutil.class); Mainactivity. This. StopService (Intent); }}View Code
Broadcastreceiverutil.java
PackageCom.genwoxue.broadcastservice;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent; Public classBroadcastreceiverutilextendsbroadcastreceiver{@Override Public voidonreceive (Context context,intent Intent) {//The Broadcast receiver (receiver) determines that the action is "Com.genwoxue.action.ABOUTSERVICE" and starts the service if("Com.genwoxue.action.ABOUTSERVICE". Equals (Intent.getaction ())) {Context.startservice (NewIntent (Context,serviceutil.class)); } }}View Code
Serviceutil.java
PackageCom.genwoxue.broadcastservice;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classServiceutilextendsservice{Private Static FinalString tag= "Aboutservice"; @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {log.i (TAG,"Service: OnCreate ()"); } //Start@Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {log.i (TAG,"Service startup: OnStart () =>intent" +intent+ ", startid=" +Startid); returnService.start_continuation_mask; } @Override Public voidOnDestroy () {log.i (TAG,"Service: OnDestroy ()"); }}View Code
Register in Androidmanifest.xml:
<android:name= "Com.genwoxue.broadcastservice.ServiceUtil"/>
Operation Result:
Reference: http://blog.csdn.net/jianghuiquan/article/details/8641152#
Android Service (ii)