0% found this document useful (0 votes)
13 views5 pages

Faheem Android

The document contains an assignment submitted by Faheem Gul for Mobile Application Development, detailing a MusicService class that plays a ringtone in the background and updates a notification with playback progress. It also includes a MainActivity class that plays the default ringtone when the application is launched. The code demonstrates the use of Android services, media playback, and notification management.

Uploaded by

moeezshehzad26
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)
13 views5 pages

Faheem Android

The document contains an assignment submitted by Faheem Gul for Mobile Application Development, detailing a MusicService class that plays a ringtone in the background and updates a notification with playback progress. It also includes a MainActivity class that plays the default ringtone when the application is launched. The code demonstrates the use of Android services, media playback, and notification management.

Uploaded by

moeezshehzad26
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/ 5

Assignment # 1

Name : Faheem gul


Roll # : 2217
Subject : Mobile Application Development
Discipline : BSCS 6th Semester

Submitted To
Lec Fareed Ullah
Govt. Superior Science College Peshawar
package com.example.servicesapp;

import android.app.*;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.*;

import androidx.annotation.RequiresApi;

import androidx.core.app.NotificationCompat;

public class MusicService extends Service {


private MediaPlayer mediaPlayer;
private Handler handler;
private int progress = 0;

private static final String CHANNEL_ID =


"ringtone_channel";
private static final int NOTIF_ID = 1;

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();

Uri uri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE
);
mediaPlayer = MediaPlayer.create(this, uri);
mediaPlayer.setLooping(true);

handler = new Handler(Looper.getMainLooper());


createNotificationChannel();
}

@Override
public int onStartCommand(Intent intent, int flags, int
startId) {
mediaPlayer.start();
startForeground(NOTIF_ID,
getNotification(progress));
updateNotificationProgress();
return START_STICKY;
}

private void updateNotificationProgress() {


handler.postDelayed(new Runnable() {
@Override
public void run() {
progress++;
Notification notification =
getNotification(progress);
NotificationManager manager =
getSystemService(NotificationManager.class);
manager.notify(NOTIF_ID, notification);

handler.postDelayed(this, 1000);
}
}, 1000);
}

private Notification getNotification(int seconds) {


return new NotificationCompat.Builder(this,
CHANNEL_ID)
.setContentTitle("Playing Ringtone")
.setContentText("Progress: " + seconds +
"s")
.setSmallIcon(android.R.drawable.ic_media_p
lay)
.setOnlyAlertOnce(true)
.setOngoing(true)
.build();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationChannel channel = new
NotificationChannel(
CHANNEL_ID,
"Ringtone Player",
NotificationManager.IMPORTANCE_LOW
);
NotificationManager manager =
getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}

@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
handler.removeCallbacksAndMessages(null);
}

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

MainActivity.java code
package com.example.servicesapp;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Ringtone ringtone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Get the default ringtone URI


Uri ringtoneUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE
);

// Get the Ringtone instance


ringtone =
RingtoneManager.getRingtone(getApplicationContext(),
ringtoneUri);

// Play it
if (ringtone != null && !ringtone.isPlaying()) {
ringtone.play();
}
}

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

// Stop the ringtone if it's still playing


if (ringtone != null && ringtone.isPlaying()) {
ringtone.stop();
}
}
}

You might also like