Categories: ANDROID APP

Easy methods to create Service class to run a {custom} alarm clock in your Android? – Full supply code



On this video it exhibits the steps to create the Service class in your Android App. It takes the instance of implementing an Alarm clock within the service class. For setting the time for the alarm, timepicker widget is used. Please discover the supply code beneath.

We might be glad to listen to from you relating to any question, ideas or appreciations at: programmerworld1990@gmail.com

https://programmerworld.co/android/how-to-create-service-class-to-run-a-custom-alarm-clock-in-your-android-complete-source-code/

bundle com.instance.myserviceclass;

import androidx.appcompat.app.AppCompatActivity;

import android.content material.Intent;
import android.os.Bundle;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

personal TimePicker timePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
tremendous.onCreate(savedInstanceState);
setContentView(R.structure.activity_main);

timePicker = findViewById(R.id.timPicker);

closing Intent intent = new Intent(this, MyService.class);
ServiceCaller(intent);

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int i, int i1) {
ServiceCaller(intent);
}
});

}

personal void ServiceCaller(Intent intent){

stopService(intent);

Integer alarmHour = timePicker.getCurrentHour();
Integer alarmMinute = timePicker.getCurrentMinute();

intent.putExtra(“alarmHour”, alarmHour);
intent.putExtra(“alarmMinute”, alarmMinute);

startService(intent);
}
}

/********************/

bundle com.instance.myserviceclass;

import android.app.Service;
import android.content material.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

personal Integer alarmHour;
personal Integer alarmMinute;
personal Ringtone ringtone;
personal Timer t = new Timer();

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

alarmHour = intent.getIntExtra(“alarmHour”, 0);
alarmMinute = intent.getIntExtra(“alarmMinute”, 0);

ringtone = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));

t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Calendar.getInstance().getTime().getHours() == alarmHour &&
Calendar.getInstance().getTime().getMinutes() == alarmMinute){
ringtone.play();
}
else {
ringtone.cease();
}

}
}, 0, 2000);

return tremendous.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
ringtone.cease();
t.cancel();
tremendous.onDestroy();
}
}

/****************/

appsrcmainreslayoutactivity_main.xml

TimePicker
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:id=”@+id/timPicker”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”

/********************/

appsrcmainAndroidManifest.xml

utility
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@fashion/AppTheme”
service android:title=”.MyService”/
exercise android:title=”.MainActivity”
intent-filter
motion android:title=”android.intent.motion.MAIN” /

class android:title=”android.intent.class.LAUNCHER” /
/intent-filter
/exercise
/utility

/manifest

source

linda

Recent Posts

The Ultimate Guide to YouTube to MP4 Converters

A YouTube to MP4 conversion software is a software or on-the-web tool designed to download…

2 days ago

Unox Equipment: A Chef’s Companion

Cooking is an art, and each artist needs the right equipment to bring their masterpiece…

2 days ago

Comparing Electric Scooters: Features and Performance

Hey there, scooter enthusiast! Whether you're a seasoned rider or just thinking about zipping around…

2 days ago

Discovering Affordable Massage Services throughout Busan

Hey there, massage fans! If you're in Busan and also searching for a way to…

3 days ago

Guide to Aroma Massage Services in Busan

Hey there! If you're planning a trip to Busan, or if you're already here and…

3 days ago

Is Instant Famous Worth It? A Detailed Review of Their Follower Services

Are you looking for ways to get more followers on your social media accounts to…

3 days ago