Lab 3b - Android Service: SKR4307-Mobile Application

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SKR4307- Mobile Application

Lab 3b - Android Service

By: Nurul Aisyah Binti Ahmad Zuhudi

Matric Number: 193904


MainActivity.java
package com.example.androidserviceexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


//Button objects
private Button buttonStart;
private Button buttonStop;

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

//Getting buttons from xml


buttonStart = (Button)findViewById(R.id.btnStart);
buttonStop = (Button)findViewById(R.id.btnStop);

//Attaching onClicklistener to object button


buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);

}
@Override
public void onClick(View view) {
if(view == buttonStart){
//Start the service here
startService(new Intent(this, MyService.class));
super.onStart();
Toast.makeText(MainActivity.this,"Your service has started",
Toast.LENGTH_SHORT).show();

} else if (view == buttonStop) {


//Stop the service here
stopService(new Intent(this, MyService.class));
onStop();
Toast.makeText(MainActivity.this,"Your service has stopped",
Toast.LENGTH_SHORT).show();
}
}

}
MyService.java
package com.example.androidserviceexample;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import androidx.annotation.Nullable;

class MyService extends Service {


//Create a mediaplayer object

private MediaPlayer player;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Getting system default ringtone

player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);

//Setting loop play to true

player.setLooping(true);

//Starting the player


player.start();

//Service will be explicity started and stopped


return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();

//Stop the player when service is destroyed


player.stop();
}

}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:orientation="vertical"
tools:ignore="RtlCompat,UselessParent">

<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start_service" />

<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/stop_service" />

</LinearLayout>

</RelativeLayout>
Strings.xml
<resources>
<string name="app_name">AndroidServiceExample</string>
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidserviceexample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

<!-- Define the service class -->


<service android:name=".MyService"/>

</application>

</manifest>
Expected Results

You might also like