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
Have you ever found yourself wishing you could dive into the thrilling world of Westeros,…
In this digital age, preserving memories has become more accessible than ever. However, many of…
In the ever-evolving landscape of party games, where humor and creativity collide, the "Horrible Therapist"…
Football visors are transparent or tinted shields attached to the helmet's face mask. They're primarily…
Introduction to Beijing Beijing, the capital of China, is a city that beautifully intertwines the…
Introduction to Bangla Newspaper Industry The Bangla newspaper sector has witnessed an incredible transformation over…