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

Tips for Maximizing Your QQDewa Experience

Understanding QQDewa Basics First things first, what's QQDewa all about? Simply put, QQDewa is an…

19 hours ago

Comcast Business: Unlocking Business Growth with Comcast Business

Introduction In today’s digital-first globe, reliable internet is the spine of any successful company. Comcast…

3 days ago

Unlocking Savings and Selection at Costco Business Center

Introduction: Costco Business Center is a specific branch of Costco Wholesale which focuses on serving…

5 days ago

Exploring the Features of Plinkobet Betting

Hey there! If you've stumbled upon this article, you're probably curious about Plinkobet and what…

5 days ago

Ultimate Guide to Online Poker Apps

Hey there, poker enthusiasts! Whether you're a seasoned pro or just starting to shuffle the…

6 days ago

Unlocking Trust: The Role of the Better Business Bureau in Modern Commerce

What Is the Better Business Bureau (BBB)? The Better Business Bureau (BBB) is a non-public,…

6 days ago