这里使用播放器播放音乐来做例子.直接上代码:
package org.example.service;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class MServices extends Service {
//使用该类来播放音乐
private MediaPlayer player;
/**
* DOC 该方法在生命周期中只会调用一次
*/
@Override
public void onCreate() {
if (player == null) {
//实例化播放器
player=new MediaPlayer();
try {
//设置指向的歌曲
player.setDataSource("/sdcard/1.mp3");
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onCreate();
}
/**
* DOC 该方法在生命周期中会被多次掉的用
* (使用activity.startService()方法)
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
if (extras != null) {
//根据不同的请求来处理动作
int data = extras.getInt("data");
switch (data) {
case 1:
play();
break;
case 2:
pause();
break;
case 3:
stop();
break;
default:
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
private void stop() {
if (player != null) {
System.out.println("stop");
player.stop();
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void pause() {
if (player != null && player.isPlaying()) {
System.out.println("pause");
player.pause();
}
}
private void play() {
if (player != null && !player.isPlaying()) {
System.out.println("play");
player.start();
}
}
@Override
public void onDestroy() {
if (player != null) {
player.stop();
player.release();
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
package org.example.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 ServicesActivity extends Activity implements OnClickListener {
private Button play, pause, stop, close, exit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
close = (Button) findViewById(R.id.close);
exit = (Button) findViewById(R.id.exit);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
close.setOnClickListener(this);
exit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//启用service
Intent intent = new Intent(this, MServices.class);
int i = 0;
if (v.getId() == play.getId()) {
i = 1;
} else if (v.getId() == pause.getId()) {
i = 2;
} else if (v.getId() == stop.getId()) {
i = 3;
} else if (v.getId() == close.getId()) {
stopService(intent);
} else if (v.getId() == exit.getId()) {
this.finish();
}
if (i != 0) {
intent.putExtra("data", i);
startService(intent);
}
}
}
<?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/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="play" />
<Button
android:id="@+id/pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pause" />
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stop" />
<Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="close" />
<Button
android:id="@+id/exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="exit" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ServicesActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MServices"></service>
</application>
</manifest>