案例:
<Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:onClick="doStart" android:text="Start Service"/> <Button android:layout_marginTop="20dp" android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:onClick="doStop" android:text="Start Service"/>
public void doStart(View view){ Intent intent = new Intent(this,SampleService.class); startService(intent); } public void doStop(View view){ Intent intent = new Intent(this,SampleService.class); stopService(intent); }
public class SampleService exteds Service{ public void onCreate(){ Log.d(tag,"onCreate") super.onCreate(); } public int onStartCommand(Intent intent,int flags,int startId){ Log.d(tag,"onStartCommand"); return super.onStartCommand(intent,flags,startId); } public void onDestroy(){ Log.d(tag,"onDestroy"); super.onDestroy(); } }
<service android:name="com.edu.hpu.cn.SampleService"></service>
案例:
MainActivity:
package com.edu.hpu.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startMusic(View view){ Intent intent = new Intent(this,PlayerMusicService.class); startService(intent); } @Override protected void onDestroy() { // TODO Auto-generated method stub Intent intent = new Intent(this,PlayerMusicService.class); stopService(intent); super.onDestroy(); } }布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="136dp" android:onClick="startMusic" android:text="播放音乐" /> </RelativeLayout>
Service类:
package com.edu.hpu.service; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Environment; import android.os.IBinder; public class PlayerMusicService extends Service{ private MediaPlayer player; @Override public void onCreate() { try { player = new MediaPlayer(); player.reset(); player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/Groove Coverage - She.mp3"); player.prepare(); player.start(); } catch (IllegalArgumentException e) { // TODO: handle exception e.printStackTrace(); }catch(SecurityException e){ e.printStackTrace(); } catch(IllegalStateException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } @Override public void onDestroy() { // TODO Auto-generated method stub player.release(); player = null; super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
配置:
<service android:name="com.edu.hpu.service.PlayerMusicService"></service>
关于MediaPlayer MediaPlayer支持主流的音频、视频文件的播放,亦支持播放 非本机的媒体文件; MediaPlayer会开启子线程播放歌曲; 可调用pause()方法暂停播放,调用seekTo()方法快进到指定的 位置开始播放; 可调用prepareAsync()方法加载歌曲,并配置OnPreparedListener, 在监听器用调用MediaPlayer的start()方法; 通常为MediaPlayer配置OnCompletionListener,以实现在播放完成后的处理;