1. 程式人生 > >Android Service的使用方法 音樂播放器例項

Android Service的使用方法 音樂播放器例項

Service翻譯成中文是服務,熟悉Windows 系統的同學一定知道很熟悉了。Android裡的Service跟Windows裡的Service功能差不多,就是一個不可見的程序在後臺執行,避免被使用者誤關閉。因為Android在某些情況下會自動關閉非前臺顯示的Activity,所以如果要讓一個功能在後臺一直執行,不被Android系統關閉,比如說鬧鐘、後臺播放音樂,就必須使用Service.
之前開發音樂播放器的時候也沒用Service,但是卻可以後臺播放,以為Service沒什麼用,但是經過一段時間後發現,沒用Service的播放器在播放一段時間後會被系統自動關閉,而且就算還在後臺播放,過一段時間後開啟播放器,再點播放按鈕,會出現兩種聲音,今天用Service寫了個Demo,完美解決以上問題。
佈局XML,就兩個按鈕:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/start"  
    android:layout_width
="fill_parent" android:layout_height="wrap_content" android:text="開始" />
<Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout>

主程式main.java:


package com.pocketdigi.service;


import
android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class main extends Activity { /** Called when the activity is first created. */ Button start,stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); start.setOnClickListener(startlis); stop.setOnClickListener(stoplis); } private OnClickListener startlis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(main.this, Music.class)); //啟動服務 } }; private OnClickListener stoplis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub stopService(new Intent(main.this,Music.class)); //停止服務 } }; public void findView(){ start=(Button)findViewById(R.id.start); stop=(Button)findViewById(R.id.stop); } }

服務 Music.java:


package com.pocketdigi.service;


import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class Music extends Service {
    private MediaPlayer mp;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mp=MediaPlayer.create(this,R.raw.xrx);
        
    }
    @Override  
    public void onStart(Intent intent, int startId) {
    	super.onStart(intent, startId);
        mp.start(); 
    } 
    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.stop();
    }

}

服務還需要在AndroidManifest.xml註冊後才能使用:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.pocketdigi.service"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <service android:enabled="true" android:name=".Music" /> 
    </application>
</manifest>