I have a notification(with broadcast receiver), alarm manager, and a time bicker. Everything is working pretty well, but I want to save the time given by the time bicker to the shared preference so when the user chooses a time from the time bicker it will always send the notification at that time. But before that, I want to set a default time ( for a notification to be sent to the user at that default time until the user changes it to the chosen time by time bicker) and save it to the same key in shared preference.
so what I want is:
I don't know how to use the shared pref with the alarm manager so any help would be appreciated...
PC it is ok to use Java and Koltin only
Android Manifast.xml
<receiver android:name=".AthanBrodcasts.fajrBroadcastReceiver"/>
My broadcast receiver
class fajrBroadcastReceiver : BroadcastReceiver() {
val CHANNEL_ID = "أذان الفجر"
override fun onReceive(context: Context?, intent: Intent?) {
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent : PendingIntent = PendingIntent.getActivity(context,0,intent,0)
val builder = NotificationCompat.Builder(context!!, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_fajr_icon)
.setContentTitle("صلاة الفجر قد حلت")
.setContentText("بسم الله الرحمن الرحيم")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(context)) {
notify(123, builder.build())
}
}
}
My Setting Activity
private lateinit var picker : MaterialTimePicker
private lateinit var calender : Calendar
val CHANNEL_ID = "أذان الفجر"
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent
fajrEditTxt.setOnClickListener {
showTimePicker()
}
fajrSalah.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked){
}else{
}
}
private fun showTimePicker(){
val fajrtime = getSharedPreferences("saveSettingData", Context.MODE_PRIVATE)
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(fajrtime.getInt("fajrTimeHour", 0)) // I took it from a library I use
.setMinute(fajrtime.getInt("fajrTimeMinutes",0)) // I took it from a library I use
.setTitleText("حدد الموعد الذي تريدة")
.build()
picker.show(supportFragmentManager, "AdhanNotifacations")
picker.addOnPositiveButtonClickListener {
if(picker.hour > 12){
fajrEditTxt.setHint(String.format("%02d",picker.hour - 12) + ":"
+ String.format("%02d", picker.minute) + " PM")
}else{
fajrEditTxt.setHint(String.format("%02d",picker.hour) + ":"
+ String.format("%02d", picker.minute) + " AM")
}
calender = Calendar.getInstance()
calender[Calendar.HOUR_OF_DAY] = picker.hour
calender[Calendar.MINUTE] = picker.minute
calender[Calendar.SECOND] = 0
calender[Calendar.MILLISECOND] = 0
alarmFajr()
}
}
private fun createNotificationChannel(){
val channelName = "مؤذن الفجر"
val DESCRIPTION_TEXT = "أشعار لأذان الفجر"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = channelName
val description_text = DESCRIPTION_TEXT
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = description_text
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
fun alarmFajr(){
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val i = Intent(this, fajrBroadcastReceiver::class.java)
pendingIntent = PendingIntent.getBroadcast(this, 0, i,0)
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP, calender.timeInMillis,
AlarmManager.INTERVAL_DAY, pendingIntent
)
}
Following is sample byte code on how to write Data in Shared Preferences:
// Storing data into SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
// Creating an Editor object to edit(write to the file)
SharedPreferences.Editor myEdit = sharedPreferences.edit();
// Storing the key and its value as the data fetched from edittext
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));
// Once the changes have been made,
// we need to commit to apply those changes made,
// otherwise, it will throw an error
myEdit.commit();
Following is the sample byte code on how to read Data in Shared Preferences:
// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);
// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);
// We can then use the data
name.setText(s1);
age.setText(String.valueOf(a));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.