Service是沒有UI的,它永遠是在背景工作
練習題目:當執行APP上的播放按鈕後,會開始播放音樂,按下停止就不會繼續播放
Step 1.在eclipse建立一個android project
File -> New -> Other -> Android Project
Step 2.專案建立完成後,建立UI
res -> layout -> main.xml
加入兩個button[紅色字體]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnStartService" //service ID & name參見R.java
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Service" //模擬器上button秀出來的名稱
/>
<Button
android:id="@+id/btnStopService" //service ID & name參見R.java
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Service" //模擬器上button秀出來的名稱
/>
</LinearLayout>
Step 3.新增一個class for 繕寫service在eclipse右邊的package explorer專案中的src,點一下箭頭處,展開src,下面的xxx.xxx,點一下箭頭處,展開xxx.xxx,按滑鼠右鍵New -> Class
Name: Servicetest[第一個字要大寫]
Superclass: android.app.Service
如果Superclass用key的沒反應請在右邊的browse點進去再choose a type處key in
完成後按下finish就可以看到多一個Servicetest.java
Step 4.開啟src -> xxx.xxx -> Service.java加入以下程式碼
package com.nash.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Service_without_UI extends Activity {
private Button btnStartService;
private Button btnStopService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Service_without_UI.this,Servicetest.class);
startService(i);
}
});
btnStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Service_without_UI.this, Servicetest.class);
stopService(i);
}
});
}
}
Step 5.開啟新增的Servicetest.java加入以下程式碼/**
*
*/
package com.nash.service;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
//import android.util.Log;
import android.widget.Toast;
/**
* @author nash
*
*/
public class Servicetest extends Service {
MediaPlayer m;
/* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "Service start", Toast.LENGTH_SHORT).show();
if (m!=null) m.stop();
m = MediaPlayer.create(this, R.raw.deal); //raw是放mp3的資料夾,deal是mp3的檔名
m.start();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service stop", Toast.LENGTH_SHORT).show();
m.stop();
m.release();
}
}
Step 6.開啟AndroidManifest.xml加入以下程式碼<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nash.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Service_without_UI"
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:name=".Servicetest"
android:label="@string/app_name">
</service>
</application>
</manifest>
參考資料:http://www.kfsoft.info/article-40-9--%E5%BB%BA%E7%AB%8BService.html
http://www.jollen.org/blog/2009/01/jollen-android-programming-8.html