0% found this document useful (0 votes)
145 views7 pages

Practical No.14

The document describes creating and using a background service in Android. It includes XML layout files, Java code for an activity and service, and instructions to declare the service in the manifest. The activity contains buttons to start and stop a media player service that loops the default ringtone.

Uploaded by

vedantkale362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views7 pages

Practical No.14

The document describes creating and using a background service in Android. It includes XML layout files, Java code for an activity and service, and instructions to declare the service in the manifest. The activity contains buttons to start and stop a media player service that loops the default ringtone.

Uploaded by

vedantkale362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No.

14
1.Strings.xml
<resources>
<string name="app_name">Services_In_Android</string>
<string name="heading">Services In Android</string>
<string name="startButtonText">Start the Service</string>
<string name="stopButtonText">Stop the Service</string>
</resources>
2.Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#168BC34A"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:ignore="MissingConstraints">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="170dp"
android:fontFamily="@font/roboto"
android:text="@string/heading"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/holo_green_dark"
android:textSize="36sp"
android:textStyle="bold" />

<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#4CAF50"
android:fontFamily="@font/roboto"
android:text="@string/startButtonText"
android:textAlignment="center"

android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="@+id/stopButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#4CAF50"
android:fontFamily="@font/roboto"
android:text="@string/stopButtonText"
android:textAlignment="center"

android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
app:srcCompat="@drawable/banner" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
3.NewService.java for media Player
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import androidx.annotation.Nullable;

public class NewService extends Service {

// declaring object of MediaPlayer


private MediaPlayer player;

@Override

// execution of service will start


// on calling this method
public int onStartCommand(Intent intent, int flags, int startId) {

// creating a media player which


// will play the audio of Default
// ringtone in android device
player = MediaPlayer.create( this,
Settings.System.DEFAULT_RINGTONE_URI );

// providing the boolean


// value as true to play
// the audio on loop
player.setLooping( true );

// starting the process


player.start();

// returns the status


// of the program
return START_STICKY;
}

@Override

// execution of the service will


// stop on calling this method
public void onDestroy() {
super.onDestroy();

// stopping the process


player.stop();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
4.MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

// declaring objects of Button class


private Button start, stop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

// assigning ID of startButton
// to the object start
start = (Button) findViewById( R.id.startButton );

// assigning ID of stopButton
// to the object stop
stop = (Button) findViewById( R.id.stopButton );

// declaring listeners for the


// buttons to make them respond
// correctly according to the process
start.setOnClickListener( this );
stop.setOnClickListener( this );
}

public void onClick(View view) {

// process to be performed
// if start button is clicked
if(view == start){

// starting the service


startService(new Intent( this, NewService.class ) );
}

// process to be performed
// if stop button is clicked
else if (view == stop){

// stopping the service


stopService(new Intent( this, NewService.class ) );

}
}
}
5.mentioning the service name in the AndroidManfiest
<service android:name=".NewService"/>

Output:

You might also like