The implementation logic is as follows:
The left side is the execution logic in the activity, and the right side is the execution logic in the service:
/** * < function description > Data interaction between service and activity, as follows: 1. Get the data source from the service, pass it to the activity; 2. * Updating of data in activity; 3. The OnCreate () in the service executes in the UI thread, and the delay needs to be executed in the child thread; * * @author Administrator */public class Mainactivity extends Activity {pri vate static final String TAG = "Demo"; Private TextView mtvcontent; Private Intent mintent; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initview (); Registerreceiver (); InitData (); }/** * < function description > Initialize view * @return void [return type description] */private void Initview () {setcontent View (R.layout.activity_main); Mtvcontent = (TextView) Findviewbyid (r.id.tv_data); }/** * < function description > initialization data * @return void [return type description] */private void InitData () {mintent = New Intent (Mainactivity.this, Countservice.class); StartService (mintent); }/** * < function description > Register broadcast * * @return void [return type description] */ private void Registerreceiver () {Intentfilter intentfilter = new Intentfilter (); Intentfilter.addaction ("Com.spt.activity.CountService"); MainActivity.this.registerReceiver (New Broadcastreceiver () {@Override public void onreceive (Context Context, Intent Intent) {Bundle bundle = Intent.getextras (); Mtvcontent.settext (bundle. GetInt ("Com.spt.CountService.count_data") + ""); LOG.D (TAG, "count_data=" + mtvcontent.gettext ()); }}, Intentfilter); } @Override protected void OnDestroy () {Super.ondestroy (); if (true) {StopService (mintent); } }}
Need to register in mainactivity to receive service sent data update broadcast: Com.spt.activity.CountService, and get the updated data, display.
Also note that after you turn on the service, you also need to stop the service. Otherwise, the service will always run in the background.
It is also found that using this method can also stop the service:
StopService (New Intent (Mainactivity.this, Countservice.class));
The Countservice service code is as follows:
public class Countservice extends Service {private int countservice = 0; Private Boolean isservicerunning = false; @Override public void OnCreate () {super.oncreate (); Isservicerunning = true; Create a child thread as Count new thread (new Runnable () {@Override public void run () {while (I sservicerunning) {try {thread.sleep (1000); } catch (Interruptedexception e) {e.printstacktrace (); } countservice++; Intent Intent = new Intent (); Intent.putextra ("Com.spt.CountService.count_data", Countservice); Intent.setaction ("Com.spt.activity.CountService"); The service sends broadcast sendbroadcast (intent); }}). Start (); } @Override Public IBinder onbind (Intent Intent) {return null; } @Override public void OnDestroy () {Super.ondestroy (); Isservicerunning = false; Countservice = 0; }}
Summary and questions:
1. Using broadcast broadcast to realize the data interaction between service and host, it is easy to cause the problem of low performance;
2. The time uncertainty factor of broadcast transmission causes the data interaction to be delayed;
(Android data transfer) between service and activity-with broadcastreceiver--data transfer