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

Top 5 Exciting Slots to Play Today

In our extensive research and analysis, we have identified the top 5 exciting slots that…

5 days ago

Top Unblocked Snowboard Games to Play

Before we jump into the list, you might be wondering why you should consider playing…

5 days ago

Choosing the Right Commercial Furniture for Your Business

Commercial furniture plays a crucial role in defining the atmosphere and efficiency of your workspace.…

5 days ago

Loaner Car: Everything You Need to Know About a Loaner Car

What Is a Loaner Car? A loaner car is a temporary replacement vehicle provided by…

6 days ago

The Biggest Poker Prizes in History

Introduction Poker has long been more than just a card game—it’s a world where strategy,…

1 week ago

Affordable and Flexible Auto Loans at Haverhill Bank

Buying a vehicle gives you freedom, convenience, and flexibility. Whether it’s an initial car or…

1 week ago