Android growth diary-learning of Service components of four Android Components
After reading the materials, the following is the related information of the Service: Definition: running in the background, invisible, no interface priority is higher than the purpose of the Activity: play music, record changes in the location of geographic information, listen for an action ..... note: When running in the main thread, you cannot use it for time-consuming requests or actions. You can open a thread in the Service to perform time-consuming operations in the thread. Type: 1. startService stopService stopSelf stopSelfResult bindService unbindService 2. the IBinder interface Start method is defined between programs in the Remote Service Android system. the Service has no contact with the startup source. 2. unable to obtain service object Bind Mode features 1. through the Ibinder interface instance, return a ServiceConnection object to the startup source 2. you can obtain the Service object through the ServiceConnection object method. the following section uses a demo to describe the Service: & steps for implementing the service: 1. create a class to inherit the Service and complete the necessary methods; 2. register in the AndroidMinifast file. before calling this, the editor will describe how to start Servie through startService.
Package com. demo. internet. musicapp; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. util. log;/*** Created by monster on 2015/7/2. * Start a service. Features of this service: * 1. there is no contact between the service and the startup source * 2. the Service object */public class MusicService extends Service {@ Override public void onCreate () {Log. I ("info", "Service -- onCreate ()"); super. onCreate () ;}@ Override public IBinder onBind (Intent intent) {Log. I ("info", "Service -- onBind ()"); return null ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {Log. I ("info", "Service -- onStartCommand ()"); return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {Log. I ("info", "Service -- onDestroy ()"); super. onDestroy ();}}
Call: intent1 = new Intent (MainActivity. this, MyStartService. class); startService (intent1); startActivity completion ------------ the following small series use the bindService method to play music demo ①. layout is not described ②. create BindMusicService. java inherits services and registers them.
Package com. demo. internet. musicapp; import android. app. service; import android. content. intent; import android. content. serviceConnection; import android. media. mediaPlayer; import android. OS. binder; import android. OS. IBinder; import android. util. log;/*** Created by monster on 2015/7/2. * method features: * 1. through the Ibinder interface instance, return a ServiceConnection object to the startup source * 2. you can obtain the Service object */public class BindMusicService extends Service {private MediaPlayer mPlayer through ServiceConnection object related methods; // declare a mediaPlayer object @ Override public IBinder onBind (Intent intent) {Log. I ("info", "BindService -- onBind ()"); return new MyBinder () ;}@ Override public void unbindService (ServiceConnection conn) {Log. I ("info", "BindService -- unbindService ()"); super. unbindService (conn) ;}@ Override public void onCreate () {Log. I ("info", "BindService -- onCreate ()"); super. onCreate (); mPlayer = MediaPlayer. create (getApplicationContext (), R. raw. meizu_music); // instantiate the object // you can set to play the mPlayer repeatedly. setLooping (true) ;}@ Override public void onDestroy () {Log. I ("info", "BindService -- onDestroy ()"); super. onDestroy (); mPlayer. stop () ;}// binderService public class MyBinder extends Binder {public BindMusicService getService () {return BindMusicService can be obtained only by inheriting Binder. this ;}} public void Play () {Log. I ("info", "play"); mPlayer. start ();} public void Pause () {Log. I ("info", "pause"); mPlayer. pause ();}}
③. Create the ServiceConnection interface in MainActivity and implement the unimplemented method, then create the BindMusicService declaration, and call the bindService method during the call.
Package com. demo. internet. musicapp; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. view. view; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements View. onClickListener {private Button btn_start, btn_stop, bind_btn_start, bind_btn_stop, bind_btn_play, struct; Intent intent1; Intent intent2; BindMusicService service; ServiceConnection con = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName, IBinder binder) {// The service automatically calls back service = (BindMusicService. myBinder) binder ). getService () ;}@ Override public void onServiceDisconnected (ComponentName name) {// calls back automatically when the service is disconnected from the startup source}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView ();} private void initView () {btn_start = (Button) findViewById (R. id. btn_start); btn_stop = (Button) findViewById (R. id. btn_stop); bind_btn_start = (Button) findViewById (R. id. bind_btn_start); bind_btn_stop = (Button) findViewById (R. id. bind_btn_stop); bind_btn_play = (Button) findViewById (R. id. bind_btn_play); bind_btn_pause = (Button) findViewById (R. id. bind_btn_pause); // bind the listening event btn_start.setOnClickListener (this); btn_stop.setOnClickListener (this); listener (this );} @ Override public void onClick (View v) {switch (v. getId () {case R. id. btn_start: // intent1 = new Intent (MainActivity. this, MusicService. class); // startService (intent1); break; case R. id. btn_stop: // stopService (intent1); break; case R. id. bind_btn_start: intent2 = new Intent (MainActivity. this, BindMusicService. class); bindService (intent2, con, BIND_AUTO_CREATE); // bind the service break; case R. id. bind_btn_play: service. play (); break; case R. id. bind_btn_pause: service. pause (); break; case R. id. bind_btn_stop: unbindService (con); // unbind the service break ;}}}