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停止服务。

 

程序运行界面:

 

代码:

定义服务,MyService.java

 



Java代码  

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

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

 



Xml代码  

 
    
  
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代码  

 
    
  
1. package
2.   
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10.   
11. public class ServicesDemo extends Activity implements
12. private static final String TAG = "ServicesDemo";  
13.   Button buttonStart, buttonStop;  
14.   
15. @Override
16. public void
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. this);  
24. this);  
25.   }  
26.   
27. public void
28. switch
29. case
30. "onClick: starting srvice");  
31. new Intent(this, MyService.class));  
32. break;  
33. case
34. "onClick: stopping srvice");  
35. new Intent(this, MyService.class));  
36. break;  
37.     }  
38.   }  
39. }


 翻译整理自:http://www.planetandroid.com/