标签:
定义:
- 后台运行,不可见,没有界面
- 优先级高于Activity
用途:
- 播放音乐、记录地理信息位置的改变、监听某种动作...
- 运行在主线程,不能用它来做耗时的请求或者动作
- 可以在服务中开一个线程,在线程中做耗时操作
类型:
1.本地服务(应用程序内部)
startService stopService stopSelf stopSelfResult
bindService unbindService
2.远程服务(Remote Service)
- Android系统内部的应用程序之间
- 定义IBinder接口
Start方式特点:
- 服务跟启动源没有任何联系
- 无法得到服务对象
Bind方式特点:
- 通过Ibinder接口实例,返回一个ServiceConnection对象给启动源
- 通过ServiceConnection对象的相关方法可以得到Service对象
<service android:name="com.example.servicedemo.MyStartService" ></service>
public class MyStartService extends Service { @Override public void onCreate() { Log.i("info", "Service--onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("info", "Service--onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { Log.i("info", "Service--onBind"); return null; } @Override public void onDestroy() { Log.i("info", "Service--onDestroy"); super.onDestroy(); } }
public void doClick(View v) { switch (v.getId()) { case R.id.start: intent1 = new Intent(MainActivity.this, MyStartService.class); startService(intent1); break; case R.id.stop: stopService(intent1); break; default: break; } }
第一次启动
onCreate()
onStartCommand()
以后启动只调用onStartCommand()
停止时调用onDestroy()
停止时也只停止一次
<service android:name="com.example.servicedemo.MyBindService" ></service>
public class MyBindService extends Service { @Override public void onCreate() { Log.i("info", "BindService--onCreate"); super.onCreate(); } @Override public IBinder onBind(Intent intent) { Log.i("info", "BindService--onBind"); return null; } @Override public void unbindService(ServiceConnection conn) { Log.i("info", "BindService--unbindService"); super.unbindService(conn); } @Override public void onDestroy() { Log.i("info", "BindService--onDestroy"); super.onDestroy(); } }
case R.id.bind: intent2 = new Intent(MainActivity.this, MyBindService.class); bindService(intent2, null, Service.BIND_AUTO_CREATE); break; case R.id.unbind: unbindService(null); break;
绑定服务时调用onCreate() onBind()方法
解绑定时调用onDestroy()方法
解绑定只能执行一次
多次点击会报错
public class MyBinder extends Binder { public MyBindService getService() { return MyBindService.this; } } @Override public IBinder onBind(Intent intent) { Log.i("info", "BindService--onBind"); return new MyBinder(); }
MyBindService service; ServiceConnection conn = new ServiceConnection() { // 当启动源跟Service的连接意外丢失的时候会调用这个方法 // 比如当Service崩溃了或者被强行杀死了 @Override public void onServiceDisconnected(ComponentName name) { } // 当启动源跟Service成功连接之后将会自动调用这个方法 @Override public void onServiceConnected(ComponentName name, IBinder binder) { service = ((MyBinder)binder).getService(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void doClick(View v) { switch (v.getId()) { case R.id.start: intent1 = new Intent(MainActivity.this, MyStartService.class); startService(intent1); break; case R.id.stop: stopService(intent1); break; case R.id.play: service.Play(); break; case R.id.pause: service.Pause(); break; case R.id.next: service.Next(); break; case R.id.bind: intent2 = new Intent(MainActivity.this, MyBindService.class); // bindService(intent2, null, Service.BIND_AUTO_CREATE); // startService(intent2); bindService(intent2, conn, Service.BIND_AUTO_CREATE); break; case R.id.unbind: // unbindService(null); unbindService(conn); break; default: break; } } @Override protected void onDestroy() { stopService(intent2); unbindService(conn); super.onDestroy(); }
标签:
原文地址:http://blog.csdn.net/duanliuchang/article/details/51351141