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>  

 

原文地址:https://www.cnblogs.com/to-creat/p/5674226.html