转载

Android成长日记-Android四大组件之Service组件的学习

1、什么是Service?

Service是Android四大组件中与Activity最相似的组件,它们都代表可执行的程序,Service与Activity的区别在于:Service一直在后台运行,它没有用户界面,所以绝不会到前台来。一旦Service被启动起来,它就与Activity一样。它完全具有自己的生命周期。

Android成长日记-Android四大组件之Service组件的学习

       A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. (来自官方解释: http://developer.android.com/guide/components/services.html

)

2.小编经过查阅资料,以下是Service的相关资料:

定义:后台运行,不可见,没有界面

优先级高于Activity

用途:

播放音乐,记录地理信息位置的改变,监听某种动作.....

注意:

运行在主线程,不能用它来做耗时的请求或者动作

可以在服务中开一个线程,在线程中做耗时操作

类型:

1.本地服务(Local Service)

应用程序内部

startService  stopService stopSelf stopSelfResult

bindService  unbindService

2.远程服务(Remote Service)

Android系统内部程序之间

定义IBinder接口

Start 方式特点

1.服务跟启动源没有任何联系

2.无法得到服务对象

Bind  方式特点

1.通过Ibinder接口实例,返回一个ServiceConnection对象给启动源

2.通过ServiceConnection对象的相关方法可以得到Service对象

3.下面小编将通过一个demo讲述Service:

&实现service的步骤:1.创建一个类继承Service,完成必要的方法;2.在AndroidMinifast文件中进行注册 3. 调用

在此之前小编将讲述一个通过startService启动Servie

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方式启动服务,这种服务的特点:  *   1.服务跟启动源没有任何联系  *   2.无法得到服务对象  */ 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();  } } 

调用:

intent1 =new Intent(MainActivity.this, MyStartService.class); startService(intent1);

Android成长日记-Android四大组件之Service组件的学习

-------------------------------------------------------------------------------------------------------------------------------------------

下面小编通过使用bindService方式来实现播放音乐的demo

①.布局不予讲述

②.创建BindMusicService.java 继承Service ,并且进行注册

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.  * 方式特点:  * 1.通过Ibinder接口实例,返回一个ServiceConnection对象给启动源  * 2.通过ServiceConnection对象的相关方法可以得到Service对象  */ public class BindMusicService extends Service {  private MediaPlayer mPlayer;  //声明一个mediaPlayer对象  @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); //实例化对象   //设置可以重复播放   mPlayer.setLooping(true);  }  @Override  public void onDestroy() {   Log.i("info", "BindService--onDestroy()");   super.onDestroy();   mPlayer.stop();  }  //必须通过继承Binder的方式才可以获得binderService服务  public class MyBinder extends Binder{   public BindMusicService getService(){    return BindMusicService.this;   }  }  public void Play(){   Log.i("info", "播放");   mPlayer.start();  }  public void Pause(){   Log.i("info", "暂停");   mPlayer.pause();  } } 

③. 在MainActivity中创建ServiceConnection接口并且实现未实现的方法,然后创建BindMusicService的声明,调用的时候bindService方法进行调用

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,bind_btn_pause;  Intent intent1;  Intent intent2;  BindMusicService service;  ServiceConnection con=new ServiceConnection() {   @Override   public void onServiceConnected(ComponentName name, IBinder binder) {    //当服务跟启动源连接的时候 会自动回调    service=((BindMusicService.MyBinder)binder).getService();   }   @Override   public void onServiceDisconnected(ComponentName name) {    //当服务跟启动源断开的时候 会自动回调   }  };  @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);   //绑定监听事件   btn_start.setOnClickListener(this);   btn_stop.setOnClickListener(this);   bind_btn_start.setOnClickListener(this);   bind_btn_stop.setOnClickListener(this);   bind_btn_play.setOnClickListener(this);   bind_btn_pause.setOnClickListener(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);//绑定服务     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);//解除绑定服务     break;   }  } } 

Android成长日记-Android四大组件之Service组件的学习

-------------------------------------------------------------------------------------------------------------------------------------------

4.效果图演示:

Android成长日记-Android四大组件之Service组件的学习

5.源码分享:

https://git.coding.net/monsterLin/MusicAppService.git

正文到此结束
Loading...