Using the broadcast call service inside the method, the indirect way to invoke the service inside the method, as in reality, please media exposure
Inside the main interface
When the interface is created, open the service Common StartService () method
Send a broadcast out
Get Intent object, come out with new
Call the setaction () method of the Intent object , Parameter: General is the package name
Call sendbroadcast (INTEBT) method, send broadcast, parameter:Intent Object
Service inside
Create a new Service class for the MyService class inheritance system
Add a custom service to the method Callservicemethod (), the method we want to invoke
Create a new internal class Myreceiver Receiver class for inheriting systems
Broadcast receivers can be registered either through the manifest file or by code, which is the only special one of the four components
Create a myreceiver member variable in myservice
When the service is created, register the broadcast recipient in the onCreate () method
Call the registerreceiver (receiver,filter) method, register the broadcast receiver, parameter:receiver is Broadcastreceiver object, created with new Myreceiver () ,filter is Intentfilter Object
Get intentfilter object, come out with new
Call the Addaction (action) method of the intentfilter object to add a listener action to the intent filter object, the action sent above
When the receiver receives the signal, it invokes the onreceive () method of the myreceiver object , in which the private method of the service can be invoked, such as Callservicemethod ()
When the service is destroyed, the registered broadcast receiver, in the OnDestroy () method, calls unresisterreceiver (receiver), Parameters: Broadcast Recipient Object
Mainactivity.java
PackageCom.tsh.broadcallservice;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Open ServiceIntent intent=NewIntent ( This, MyService.class); StartService (Intent); } //Broadcast Invoke service Public voidCall (View v) {Intent Intent=NewIntent (); Intent.setaction ("Com.tsh.broadcallservice"); Sendbroadcast (Intent); }}
Myservice.java
PackageCom.tsh.broadcallservice;ImportAndroid.app.Service;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.IBinder;ImportAndroid.widget.Toast; Public classMyServiceextendsService {Privatemyreceiver Receiver; @Override Publicibinder onbind (Intent Intent) {return NULL; } //register the broadcast recipient when the service is created@Override Public voidonCreate () {Super. OnCreate (); Receiver=NewMyreceiver (); Intentfilter Filter=NewIntentfilter (); Filter.addaction ("Com.tsh.broadcallservice"); Registerreceiver (receiver, filter); } //a method of service inside Public voidCallservicemethod () {Toast.maketext (), Getapplicationcontext (),"Broadcast Call service", 0). Show (); } //internal classes implement broadcast receivers Private classMyreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {Callservicemethod (); } } }
[Android] code to register the broadcast Receiver & method of invoking the service using the broadcast