一. Service组件
运行于主线程中, 在后台执行各种耗时较长的操作, 是android的四大组件之一
特点:
1.
没有用户界面
2. 比Activity优先级高, 不会轻易被android系统终止
3. 在系统资源恢复后Service也将自动恢复运行状态
4. 可用于进程间通信, 解决两个不同android应用程序进程之间的调用和通信问题
二. Service的两种使用方式
1. 以启动方式使用Service
启动方式:
intent=new Intent(); intent.setClass(MainActivity.this, MyService.class); startService(intent); // Context.startService(intent);
销毁方式:
// 在启用Service的组件的onDestroy()方法中停止服务 //stopService(intent); @Override protected void onDestroy() { super.onDestroy(); stopService(intent); } // 或 @Override protected void onBackPressed() { super.onBackPressed(); stopService(intent); }
Service的生命周期:
onCreate(), onStart(), onStartCommand(), onDestroy()
①. 当Service第一次创建的时候才会执行onCreate(),
以后在启动时不会执行
②. 只要服务不销毁就会一直存在
2. 以绑定方式使用Service
启动方式:
intent=new Intent(MainActivity.this,MyServiceBinder.class); bindService(intent, conn, Context.BIND_AUTO_CREATE); // Context.bindService(...);
销毁方式:
unbindService(conn);
Service的生命周期:
onCreate(), onBind(), onUnbind(), onDestroy()
①. 调用者销毁,服务立即销毁,它的生命周期和调用者绑定在一起
②.
如果调用者销毁,必须手动解绑(unbindService(conn)), 每一次运行程序, 服务都会重新开始
三. 实例代码
MainActivity.java
1 package com.anjoyo.startservice; 2 3 import com.anjoyo.startservice.MyServiceBinder.MyBinder; 4 5 import android.os.Bundle; 6 import android.os.IBinder; 7 import android.app.Activity; 8 import android.content.ComponentName; 9 import android.content.Context; 10 import android.content.Intent; 11 import android.content.ServiceConnection; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 15 public class MainActivity extends Activity { 16 17 private Intent intent; 18 MyBinder binder; 19 ServiceConnection conn= new ServiceConnection(){ 20 21 public void onServiceConnected(ComponentName name, IBinder service) { 22 //当服务 23 System.out.println("onServiceConnected()====>"+name.getPackageName()+name.getClassName()); 24 binder=(MyBinder)service; 25 System.out.println(binder.getServiceName()); 26 } 27 28 public void onServiceDisconnected(ComponentName name) { 29 30 }}; 31 public void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 35 this.findViewById(R.id.tv_startservice).setOnClickListener(new OnClickListener() { 36 37 public void onClick(View v) { 38 /* 39 //1.使用startService启动服务 40 intent=new Intent(); 41 intent.setClass(MainActivity.this, MyService.class); 42 //当服务第一次创建的时候才会执行onCreate(),以后在启动时不会执行 43 //只要服务不销毁就会一直存在 44 startService(intent); 45 */ 46 //2.使用bindService()使用服务 47 //onCreate(),onBind() 48 intent=new Intent(MainActivity.this,MyServiceBinder.class); 49 bindService(intent, conn, Context.BIND_AUTO_CREATE); 50 } 51 }); 52 } 53 54 //停止服务 55 @Override 56 protected void onDestroy() { 57 super.onDestroy(); 58 stopService(intent); 59 //调用者销毁,服务立即销毁,它的生命周期和调用者绑定在一起, 60 //如果调用者销毁,必须手动解绑, 每一次运行程序, 服务都会重新开始 61 unbindService(conn); 62 } 63 64 }
MyService.java
1 package com.anjoyo.startservice; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 7 public class MyService extends Service { 8 9 public void onCreate() { 10 System.out.println("onCreate"); 11 super.onCreate(); 12 } 13 14 @Override 15 @Deprecated 16 public void onStart(Intent intent, int startId) { 17 System.out.println("onStart"); 18 super.onStart(intent, startId); 19 } 20 21 @Override 22 public int onStartCommand(Intent intent, int flags, int startId) { 23 long sum=0; 24 for (int i = 0; i < 10000001; i++) { 25 sum+=i; 26 System.out.println(sum); 27 } 28 System.out.println("onStartCommand"); 29 return super.onStartCommand(intent, flags, startId); 30 } 31 32 @Override 33 public void onDestroy() { 34 System.out.println("onDestroy"); 35 super.onDestroy(); 36 } 37 38 public IBinder onBind(Intent intent) { 39 System.out.println("onBind"); 40 return null; 41 } 42 43 }
MyServiceBinder.java
1 package com.anjoyo.startservice; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyServiceBinder extends Service { 9 10 @Override 11 public void onCreate() { 12 System.out.println("MyServiceBind.onCreate()"); 13 super.onCreate(); 14 } 15 16 class MyBinder extends Binder{ 17 public MyServiceBinder getServiceName(){ 18 return MyServiceBinder.this; 19 } 20 } 21 22 public IBinder onBind(Intent intent) { 23 // long sum=0; 24 // for (int i = 0; i < 1000000; i++) { 25 // sum+=i; 26 // System.out.println(sum); 27 // } 28 System.out.println("MyServiceBind.onBind()"); 29 return new MyBinder(); 30 } 31 32 @Override 33 public boolean onUnbind(Intent intent) { 34 System.out.println("MyServiceBind.onUnbind()"); 35 return super.onUnbind(intent); 36 } 37 38 @Override 39 public void onDestroy() { 40 System.out.println("MyServiceBind.onDestroy()"); 41 super.onDestroy(); 42 } 43 }
原文:http://www.cnblogs.com/shangzongkang/p/service_1.html