Android Service运用总结

Android Service使用总结

Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。

特别需要注意的是Service跟Activities是不同 的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用 startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。

 

启动一个Service的过程如下:

context.startService()  ->onCreate()- >onStart()->Service running

其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。

 

停止一个Service的过程如下:

context.stopService() | ->onDestroy() ->Service stop

 

 

接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。

 

程序运行界面:

Android Service运用总结

 

代码:

定义服务,MyService.java

 

Java代码  Android Service运用总结
  1. package  com.example;  
  2.   
  3. import  android.app.Service;  
  4. import  android.content.Intent;  
  5. import  android.media.MediaPlayer;  
  6. import  android.os.IBinder;  
  7. import  android.util.Log;  
  8. import  android.widget.Toast;  
  9.   
  10. public   class  MyService  extends  Service {  
  11.     private   static   final  String TAG =  "MyService" ;  
  12.     MediaPlayer player;  
  13.       
  14.     @Override   
  15.     public  IBinder onBind(Intent intent) {  
  16.         return   null ;  
  17.     }  
  18.       
  19.     @Override   
  20.     public   void  onCreate() {  
  21.         Toast.makeText(this "My Service Created" , Toast.LENGTH_LONG).show();  
  22.         Log.d(TAG, "onCreate" );  
  23.           
  24.         player = MediaPlayer.create(this , R.raw.braincandy); //运行例子是,需要替换音乐的名称   
  25.         player.setLooping(false );  // Set looping   
  26.     }  
  27.   
  28.     @Override   
  29.     public   void  onDestroy() {  
  30.         Toast.makeText(this "My Service Stopped" , Toast.LENGTH_LONG).show();  
  31.         Log.d(TAG, "onDestroy" );  
  32.         player.stop();  
  33.     }  
  34.       
  35.     @Override   
  36.     public   void  onStart(Intent intent,  int  startid) {  
  37.         Toast.makeText(this "My Service Started" , Toast.LENGTH_LONG).show();  
  38.         Log.d(TAG, "onStart" );  
  39.         player.start();  
  40.     }  
  41. }  

 

除此之外还要在Manifest里面声明服务:

 

Xml代码  Android Service运用总结
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < manifest   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.   package = "com.example"   android:versionCode = "1"   android:versionName = "1.0" >   
  4.   < application   android:icon = "@drawable/icon"   android:label = "@string/app_name" >   
  5.     < activity   android:name = ".ServicesDemo"   android:label = "@string/app_name" >   
  6.       < intent-filter >   
  7.         < action   android:name = "android.intent.action.MAIN"   />   
  8.         < category   android:name = "android.intent.category.LAUNCHER"   />   
  9.       </ intent-filter >   
  10.     </ activity >   
  11.     < service   android:enabled = "true"   android:name = ".MyService"   />   
  12.   </ application >   
  13.   < uses-sdk   android:minSdkVersion = "3"   />   
  14. </ manifest >    
 

 定义Activity,ServicesDemo.java

 

Java代码  Android Service运用总结
  1. package  com.example;  
  2.   
  3. import  android.app.Activity;  
  4. import  android.content.Intent;  
  5. import  android.os.Bundle;  
  6. import  android.util.Log;  
  7. import  android.view.View;  
  8. import  android.view.View.OnClickListener;  
  9. import  android.widget.Button;  
  10.   
  11. public   class  ServicesDemo  extends  Activity  implements  OnClickListener {  
  12.   private   static   final  String TAG =  "ServicesDemo" ;  
  13.   Button buttonStart, buttonStop;  
  14.   
  15.   @Override   
  16.   public   void  onCreate(Bundle savedInstanceState) {  
  17.     super .onCreate(savedInstanceState);  
  18.     setContentView(R.layout.main);  
  19.   
  20.     buttonStart = (Button) findViewById(R.id.buttonStart);  
  21.     buttonStop = (Button) findViewById(R.id.buttonStop);  
  22.   
  23.     buttonStart.setOnClickListener(this );  
  24.     buttonStop.setOnClickListener(this );  
  25.   }  
  26.   
  27.   public   void  onClick(View src) {  
  28.     switch  (src.getId()) {  
  29.     case  R.id.buttonStart:  
  30.       Log.d(TAG, "onClick: starting srvice" );  
  31.       startService(new  Intent( this , MyService. class ));  
  32.       break ;  
  33.     case  R.id.buttonStop:  
  34.       Log.d(TAG, "onClick: stopping srvice" );  
  35.       stopService(new  Intent( this , MyService. class ));  
  36.       break ;  
  37.     }  
  38.   }