Create an Android application that plays an audio(song) in the background. Audio will not be stopped even if you switch to another activity. To stop the audio, you need to stop the service.
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <Button
- android:id="@+id/buttonStart"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="74dp"
- android:text="Start Service" />
- <Button
- android:id="@+id/buttonStop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="Stop Service" />
- <Button
- android:id="@+id/buttonNext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="63dp"
- android:text="Next Page" />
- </RelativeLayout>
activity_next.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="example.javatpoint.com.androidservice.NextPage">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="200dp"
- android:text="Next Page"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
MyService.java
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.widget.Toast;
- public class MyService extends Service {
- MediaPlayer myPlayer;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
- myPlayer = MediaPlayer.create(this, R.raw.sun);
- myPlayer.setLooping(false); // Set looping
- }
- @Override
- public void onStart(Intent intent, int startid) {
- Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
- myPlayer.start();
- }
- @Override
- public void onDestroy() {
- Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
- myPlayer.stop();
- }
- }
MainActivity.java
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- Button buttonStart, buttonStop,buttonNext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- buttonStart = findViewById(R.id.buttonStart);
- buttonStop = findViewById(R.id.buttonStop);
- buttonNext = findViewById(R.id.buttonNext);
- buttonStart.setOnClickListener(this);
- buttonStop.setOnClickListener(this);
- buttonNext.setOnClickListener(this);
- }
- public void onClick(View src) {
- switch (src.getId()) {
- case R.id.buttonStart:
- startService(new Intent(this, MyService.class));
- break;
- case R.id.buttonStop:
- stopService(new Intent(this, MyService.class));
- break;
- case R.id.buttonNext:
- Intent intent=new Intent(this,NextPage.class);
- startActivity(intent);
- break;
- }
- }
- }
NextPage.java
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- public class NextPage extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_next);
- }
- }
AndroidManifest.xml
- <service
- android:name=".MyService"
- android:enabled="true" />
0 Comments