The most important component services and activities of Android are the communication problems between them. Of course, today we will not study the underlying implementation issues. I will study how to use the upper-layer applications.
First, the activity calls the service
This is relatively basic, and there are two common methods;
First, using intent, this is relatively simple. You can specify the package name and class name to call it. The intent. setclassname member can be used. Use putstring to load data. startservice (intent) is an example:
Intent regintent = new intent ("com. Service ");
Regintent. putextra ("data", "hellodata ");
Startservice (regintent );
Second, through IPC, this is troublesome and generally not needed.
But, in turn, how does the service tell the activity some states? There are two methods
1. The Service sends broadcast through broadcast. we can write a broadcastreceiver. Normally, broadcastreceiver is written as an internal class of the activity. This oncaster can directly call the activity method to update the interface. However, internal classes can only use the registerreceiver () method for code registration. static declarations cannot be made in the androidmanifest. xml file, because internal classes depend on external classes. If you must use androidmanifest to register a receiver, you can only write broadcastreceiver as a public class for a separate file. At this time, it is troublesome to update the interface. You can only run the activity you want to update, and then send a message to the internal class of the activity to update the interface.
2. The Service sends intent directly to the activity. Startactivity in service is a startactivity outside activity, that is, an activity outside task. Therefore, you must add the following parameter to intent: intentsend = new intent (constants. action_status );
Intent1.addflags (intent. flag_activity_new_task );
Intent1.putextra ("statues", "end");
Context. startactivity (intent1 );
However, one problem that occurs at this time is that multiple startactivity attempts will cause many activity implementations to run. This is definitely not what we need. I only need one activity. At this time, set the launchmode of the activity to singleinstance in androidmanifest.
<Activity Android: Name ="Com. Demo. Activity"
Android: Label ="@ String/online"Android: launchmode ="Singleinstance">
Remember, someone can set it to singletask, but they have a difference.
Remember to update the intent so that getinstent can get the new instance each time.
@Overrideprotected void onNewIntent (Intent intent){ setIntent(intent);}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////
Instance:
When a service is used to download files, a problem is that the service has no interface. It is a little difficult for the service to directly transmit data to the activity to notify the user of the current download progress, so EOE thought of using broadcast, the Service Broadcast and activity are responsible for receiving and processing the received data. This achieves our goal.
Register the aggreger first, and then start the service. The textview and progressbar above will change with the value passed by the Service, and will not change after you unregister the aggreger or stop the service.
Service Code:
Package COM. services; import android. app. service; import android. content. intent; import android. OS. ibinder; import android. util. log; public class testservice extends Service {Boolean isstop = false; @ override public ibinder onbind (intent) {log. I ("tag", "bind"); return NULL;} public void oncreate () {log. I ("tag", "Services oncreate"); super. oncreate ();} public void onstart (intent, int startid ){ Log. I ("tag", "Services onstart"); super. onstart (intent, startid); New thread () {// create a thread, send a broadcast every one second, and put I into intent to output public void run () {int I = 0; while (! Isstop) {intent = new intent (); intent. putextra ("I", I); I ++; intent. setaction ("android. intent. action. test "); // the action is the same as the sendbroadcast (intent); log. I ("tag", String. valueof (I); try {sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}}. start () ;}@ override public void ondestroy () {log. I ("tag", "Services ondestory"); isstop = true; // The thread will not be stopped even if the service is destroyed. Therefore, you can set isstop to stop the super thread. ondestroy ();}}
Activity Code:
Package COM. services; import android. app. activity; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. content. intentfilter; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. progressbar; import android. widget. textview; public class main extends activity {/** called when the activity is first created. */button B1, B2, B3, B4; testservice mservice; progressbar Pb; myreceiver receiver; textview TV; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); b1 = (button) findviewbyid (R. id. b1); b2 = (button) findviewbyid (R. id. b2); B3 = (button) findviewbyid (R. id. b3); B4 = (button) findviewbyid (R. id. b4); b1.setonclicklistener (L1); b2.setonclicklistener (L2); b3.setonclicklistener (L3); b4.setonclicklistener (L4); Pb = (progressbar) findviewbyid (R. id. PB); TV = (textview) findviewbyid (R. id. TV);} public class myreceiver extends broadcastreceiver {// custom a broadcast receiver @ override public void onreceive (context, intent) {system. out. println ("onreceiver"); bundle = intent. getextras (); int A = bundle. getint ("I"); Pb. setprogress (a); TV. settext (string. valueof (a); // process received content} public myreceiver () {system. out. println ("mycycler"); // constructor for initialization, which has no function in this example} onclicklistener L1 = new onclicklistener () {@ override public void onclick (view v) {startservice (new intent (main. this, testservice. class); // start service }}; onclicklistener L2 = new onclicklistener () {@ override public void onclick (view v) {stopservice (new intent (main. this, testservice. class); // end service }}; onclicklistener l3 = new onclicklistener () {@ override public void onclick (view v) {// register the receiver er ER = new myreceiver (); intentfilter filter = new intentfilter (); filter. addaction ("android. intent. action. test "); main. this. registerreceiver (receiver, filter) ;}}; onclicklistener L4 = new onclicklistener () {@ override public void onclick (view v) {main. this. unregisterreceiver (receiver ER); // unregister the receiver }};}
Source: http://hi.baidu.com/fuckin/item/59d45694bc672d33336eebc2