I feel that the Service is a bit tangled. I don't know if it is because of books. The examples in the book are not successful, and I am confused.
We mentioned four things together. We mentioned above, naturally, the introduction of those barabara.
Important:
1. Create a Service );
In Android, Service is one of the most important elements. A "Service" involves inheritance of Service classes and related permission authorization code added to AndroidManifest. xml.
In addition, it is the basic method of the Service class, onCreate (), onBind (), onStart (), onStartCommand (), onDestory ()......
Defining the service name is important because it can make your service be used in other applications (create, start, stop, and destroy ). the onStart () and onCommand () methods are functionally consistent,
Only the former (onStart () has been abolished after API L5 and replaced by onStartCommand (). Generally, the default implementation of the latter (onStartCommand () is to call onStart (), and return a value to facilitate compatibility with the old version.
Declaration period: I> Context. startService () starts a service, onCreate ()-> onStart () or onCommand ()-> Context. stopService () or stopSelf ()-> onDestory ().
Ii> Context. bindService () is bound to a service, onCreate ()-> onBind ()-> Context. unbindService () or onUnBind ()-> stioSelf ()-> onDestory ().
It looks complicated and clear when you use it on your own.
Instance:
1 package com. qimu. service; 2 3 import android. app. service; 4 import android. content. broadcastReceiver; 5 import android. content. context; 6 import android. content. intent; 7 import android. content. intentFilter; 8 import android. OS. IBinder; 9 import android. OS. systemClock; 10 import android. util. log; 11 12 public class QiMuService extends Service {13 14 qimuReceiver recevier; 15 16 @ Override17 public IBinder onBind (Intent intent) {18 19 return null; 20} 21 22 private static final String DEBUG_TAG = "QiMu_DEBUG"; 23 24 public void onCreate () {25 26 Log. v (DEBUG_TAG, "Service starting"); 27 28 recevier = new qimuReceiver (); 29 30 super. onCreate (); 31} 32 33 public void onStart (Intent intent, int startId) {34 35 System. out. println ("Start sucess"); 36 37 IntentFilter filter = new IntentFilter (); 38 filter. addAction ("com. qimu. qiMuService "); 39 registerReceiver (recevier, filter); 40 41 for (int I = intent. getIntExtra ("_ start", 0); I <100; I ++) {42 43 SystemClock. sleep (200); 44 if (I % 5 = 0) {45 System. out. println (I); 46 Intent activityCallBack = new Intent (); 47 activityCallBack. setAction ("com. qimu. mainActivity "); 48 activityCallBack. putExtra ("_ time", I); 49 sendBroadcast (activityCallBack); 50} 51} 52 53 onDestory (); 54} 55 56 public int onStartCommand (Intent intent, int flags, int startId) {57 58 onStart (intent, startId); 59 return Service. START_REDELIVER_INTENT; 60} 61 62 public void onDestory () {63 64 super. onDestroy (); 65} 66 67 public class qimuReceiver extends BroadcastReceiver {68 69 @ Override70 public void onReceive (Context context, Intent intent) {71 //...... 72} 73 74} 75 76}SimpleService 1 package com. qimu. service; 2 3 import android. annotation. suppressLint; 4 import android. app. activity; 5 import android. content. broadcastReceiver; 6 import android. content. context; 7 import android. content. intent; 8 import android. content. intentFilter; 9 import android. OS. bundle; 10 import android. view. view; 11 import android. widget. textView; 12 13 public class MainActivity extends Activity {14 15 TextView TV _service; 16 qimuBroadcast broadcast; 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. activity_main); 22 23 TV _service = (TextView) findViewById (R. id. service); 24 25 TV _service.setOnClickListener (new View. onClickListener () {26 27 @ Override28 public void onClick (View arg0) {29 // TODO Auto-generated method stub30 // Intent serviceIntent = new Intent (MainActivity. this, com. qimu. service. qiMuService. class); 31 // serviceIntent. putExtra ("_ start", 1); 32 // startService (serviceIntent); 33 Intent intent = new Intent ("com. qimu. service. qiMuService "); 34 intent. putExtra ("_ start", 1); 35 startService (intent); 36} 37}); 38} 39 40 public void onStart () {41 42 System. out. println ("Start sucess ...... "); 43 broadcast = new qimuBroadcast (); 44 IntentFilter intentfilter = new IntentFilter (); 45 intentfilter. addAction ("com. qimu. mainActivity "); 46 registerReceiver (broadcast, intentfilter); 47 super. onStart (); 48} 49 50 public void onStop () {51 52 unregisterReceiver (broadcast); 53 super. onStop (); 54} 55 56 public class qimuBroadcast extends BroadcastReceiver {57 58 @ SuppressLint ("ShowToast") 59 @ Override60 public void onReceive (Context context, Intent intent) {61 62 int _ time = intent. getIntExtra ("_ time",-1); 63 TV _service.setText ("" + _ time); 64} 65} 66 67}ControlActivity
The example in the book is not successful, so I wrote a simple example of communication between the Service and the Activity, which seems a bit frustrating, but the general function is to implement, from Service registration, creation, start, call BroadcastReceiver to pass data, stop, and destroy all the data, which is quite simple.
2. implement remote structure (AIDL );
Of course, if you write a program with great effort, you will certainly consider the issue of "communication" with other applications (executing other calls ), therefore, it is obvious that the interface (remote interface) problem is involved. AIDL is the core to solve this problem. To define a remote interface, you must first create an AIDL file, declare the interface in the file, then implement this interface, and in onBind () when the method is called, it returns the instance of this interface (it is not clear, but the code should be: Create a class to implement this interface and return the instance of this class.
Ke, I do not know how to create an AIDL file even eclipse, but there is always a good person in a corner left an example (link: http://my.oschina.net/u/779520/blog/79936 ).
Then we can use its interface to implement the Service. At this time, we will involve two ServiceConnection Methods: onServiceConnected () and onServiceDisconnected () to achieve Service connection and release the connection (note: the remote interface call will perform cross-thread operations and the synchronization is complete. If the call takes a long time, it should be processed in another thread like other time-consuming calls ).
Note: When this interface is required in another program, you need to add the AIDL file and the corresponding package in the project. (the common point is that this interface file must be included in the involved program. One is implemented, and the others are (guests) used to call it ).
Instance:
1 package com. qimu. service; 2 3 interface IRemoteInterface {4 String getValue (); 5}Server AIDL file 1 package com. qimu. service; 2 3 import android. app. service; 4 import android. content. intent; 5 import android. OS. IBinder; 6 import android. OS. remoteException; 7 8 public class Aidl_Service extends Service {9 10 public class aidlService extends IRemoteInterface. stub {11 12 @ Override13 public String getValue () throws RemoteException {14 return "Service connection successful! "; 15} 16 17} 18 19 @ Override20 public IBinder onBind (Intent intent) {21 22 return new aidlService (); 23} 24}The Server Service implements 1 package com. qimu. service; 2 3 import android. app. activity; 4 import android. content. componentName; 5 import android. content. context; 6 import android. content. intent; 7 import android. content. serviceConnection; 8 import android. OS. IBinder; 9 import android. OS. remoteException; 10 import android. view. view; 11 import android. view. view. onClickListener; 12 import android. widget. button; 13 impo Rt android. widget. textView; 14 15 public class MainActivity extends Activity implements OnClickListener {16 17 TextView TV _service; 18 Button bt_1, bt_2; 19 private IRemoteInterface iremoteInterface = null; 20 21 private ServiceConnection serviceconnection = new ServiceConnection () {22 23 @ Override24 public void onServiceDisconnected (ComponentName arg0) {25} 26 27 @ Override28 public void onServiceConn Ected (ComponentName name, IBinder service) {29 30 iremoteInterface = IRemoteInterface. stub. asInterface (service); 31 bt_2.setEnabled (true); 32} 33}; 34 35 @ Override36 public void onClick (View view) {37 // TODO Auto-generated method stub38 switch (view. getId () {39 40 case R. id. button1: 41 42 TV _service.setText ("Service starting! "); 43 bindService (new Intent (" com. example. aidlservice. aidl_Service "), serviceconnection, Context. BIND_AUTO_CREATE); 44 break; 45 46 case R. id. button2: 47 48 try {49 TV _service.setText (iremoteInterface. getValue (); 50} catch (RemoteException e) {51 System. out. println ("Error"); 52} 53 break; 54 55 default: 56 break; 57} 58} 59 60}Client call
In a simple example, there are two buttons and one TextView in the client program. Click bt_1 to bind and enable the service, and then click bt_2 to obtain the functions implemented by the server (return a string ).
3. Implement Parcelable)
The Parcelable class is mainly used to encapsulate data and facilitate data transmission. For its practical attachment link, refer.
4. IntentService class
In programming, you will need to process a lot of high-frequency requests, put these tasks that need to be periodically executed in a work queue, it will make your program simple and efficient, at the same time, you can avoid creating a complete and troublesome Service. IntentService is a simple Service that can process asynchronous tasks using the Intent request method, each Intent will be added to the work queue related to IntentService and serialized for processing. For the returned results, you can choose to broadcast (sendBroadcast () method) send Intent objects to the application and capture these results using the BroadcastReceiver in the application.
Instance: 1 is used ......
Summary: The Service mechanism provided by the Android SDK is really powerful. An excellent Service can significantly enhance the attractiveness of applications or services provided, however, when creating a background service, you must pay attention to it, the rough design may affect the performance of the mobile terminal and the battery life of the battery in essence (so pay special attention to Service considerations during comprehensive testing ).
The second article, over the past few days, has ended and gained a lot. Come on, cool.
The end of the period is over. I have to review the course day and night. 55555555555555555555555555.