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停止服務。
ServicesDemo.java(是一個Activity)

[java]  view plain  copy
 
  1. package com.android.myservice;  
  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 ServiceDemo extends Activity implements OnClickListener {  
  12.     private static final String TAG = "ServiceDemo";  
  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.i(TAG, "onClick: starting service");  
  31.                 startService(new Intent(this, MyService.class));  
  32.                 break;  
  33.             case R.id.buttonStop:  
  34.                 Log.i(TAG, "onClick: stopping service");  
  35.                 stopService(new Intent(this, MyService.class));  
  36.                 break;  
  37.         }  
  38.     }  
  39. }  




除此之外還要在Manifest里面聲明服務:(AndroidManifest.xml)

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     package="com.android.myservice">  
  5.     <application android:label="@string/app_name">  
  6.         <activity android:name=".ServiceDemo" android:label="@string/app_name">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN"/>  
  9.                 <category android:name="android.intent.category.LAUNCHER"/>  
  10.             </intent-filter>  
  11.         </activity>  
  12.         <service android:enabled="true" android:name=".MyService"/>  
  13.     </application>  
  14. </manifest>  




定義Service(MyService.java)

[java]  view plain  copy
 
  1. package com.android.myservice;  
  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.i(TAG, "onCreate");  
  23.   
  24.         player = MediaPlayer.create(this, R.raw.braincandy);  
  25.         player.setLooping(false);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onDestroy() {  
  30.         Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();  
  31.         Log.i(TAG, "onDestroy");  
  32.         player.stop();  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onStart(Intent intent, int startid) {  
  37.         Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();  
  38.         Log.i(TAG, "onStart");  
  39.         player.start();  
  40.     }  
  41. }  



layout文件夾中是main.xml

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:gravity="center">  
  7. <TextView  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>  
  10. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="@string/start"></Button>  
  11. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonStop"></Button>  
  12. </LinearLayout>  


values 文件夾中是strings.xml

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <resources>  
  4.     <string name="start">Start</string>  
  5.     <string name="stop">Stop</string>  
  6.     <string name="services_demo">Service Demo</string>  
  7. </resources>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM