【问题标题】:How to save notification on shared preference?如何保存共享偏好的通知?
【发布时间】:2021-07-24 21:54:50
【问题描述】:

我有一个通知(带有广播接收器)、警报管理器和一个计时员。一切都运行良好,但我想将时间争吵者提供的时间保存到共享偏好中,因此当用户从时间争吵者中选择时间时,它将始终在那个时候发送通知。但在此之前,我想设置一个默认时间(在该默认时间向用户发送通知,直到用户通过时间争吵者将其更改为所选时间)并将其保存到共享偏好中的相同键。

所以我想要的是:

  1. 将默认时间保存到共享首选项,以便每天在默认时间通知用户。
  2. 将用户从时间选择器中选择的时间保存到与 #1 相同的键,因此它将成为默认时间。

我不知道如何将共享首选项与警报管理器一起使用,因此我们将不胜感激...

PC可以只使用 Java 和 Koltin

Android Manifast.xml

        <receiver android:name=".AthanBrodcasts.fajrBroadcastReceiver"/>

我的广播接收器

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())
    }

}
}

我的设置活动

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
    )
}

【问题讨论】:

    标签: java android android-studio kotlin alarmmanager


    【解决方案1】:

    以下是有关如何在共享首选项中写入数据的示例字节代码:

    // 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();
    

    以下是有关如何读取共享首选项中的数据的示例字节码:

     // 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));
    

    【讨论】:

    • 我知道它是 java,但是当你说 kotlin 时,你总是可以尝试翻译或遵循我的代码逻辑
    • 我知道如何使用共享偏好,但我不知道如何将它与警报管理器一起使用我不知道如何保存通知的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多