MusicServiceClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 MusicServiceClient extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Intent used for starting the MusicService final Intent musicServiceIntent = new Intent(getApplicationContext(), MusicService.class); final Button startButton = (Button) findViewById(R.id.start_button); startButton.setOnClickListener(new OnClickListener() { public void onClick(View src) { // Start the MusicService using the Intent startService(musicServiceIntent); } }); final Button stopButton = (Button) findViewById(R.id.stop_button); stopButton.setOnClickListener(new OnClickListener() { public void onClick(View src) { // Stop the MusicService using the Intent stopService(musicServiceIntent); } }); } } |
MusicService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.IBinder; public class MusicService extends Service { @SuppressWarnings("unused") private final String TAG = "MusicService"; private static final int NOTIFICATION_ID = 1; private MediaPlayer mPlayer; private int mStartID; @Override public void onCreate() { super.onCreate(); // Set up the Media Player mPlayer = MediaPlayer.create(this, R.raw.badnews); if (null != mPlayer) { mPlayer.setLooping(false); // Stop Service when music has finished playing mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // stop Service if it was started with this ID // Otherwise let other start commands proceed stopSelf(mStartID); } }); } // Create a notification area notification so the user // can get back to the MusicServiceClient final Intent notificationIntent = new Intent(getApplicationContext(), MusicServiceClient.class); final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); final Notification notification = new Notification.Builder( getApplicationContext()) .setSmallIcon(android.R.drawable.ic_media_play) .setOngoing(true).setContentTitle("Music Playing") .setContentText("Click to Access Music Player") .setContentIntent(pendingIntent).build(); // Put this Service in a foreground state, so it won't // readily be killed by the system startForeground(NOTIFICATION_ID, notification); } @Override public int onStartCommand(Intent intent, int flags, int startid) { if (null != mPlayer) { // ID for this start command mStartID = startid; if (mPlayer.isPlaying()) { // Rewind to beginning of song mPlayer.seekTo(0); } else { // Start playing song mPlayer.start(); } } // Don't automatically restart this Service if it is killed return START_NOT_STICKY; } @Override public void onDestroy() { if (null != mPlayer) { mPlayer.stop(); mPlayer.release(); } } // Can't bind to this Service @Override public IBinder onBind(Intent intent) { return null; } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/stop_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/stop_service_string" android:textSize="24sp" > </Button> <Button android:id="@+id/start_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/stop_button" android:text="@string/start_service_string" android:textSize="24sp" > </Button> </RelativeLayout> |