如何在Kotlin中设置多个警报?

时间:2018-11-07 22:20:11

标签: android kotlin android-alarms

所以我是编码的新手,我正在为穆斯林的祈祷时间制作一个应用程序,我希望在这些时间重复设置警报 我尝试了很多事情,但是没有用!

我有一个祈祷课,如果有人愿意,我会把它放

如果您能帮助我为一次祈祷设定一个闹钟,以便我能做剩下的事情,我会更加感激。

主要活动

    val latitude = 30.354802
    val longitude = 42.2461069
    val timezonoffset = 3.0
    val timeZoneId = "Middle East/Alowiqila"
    val prayTimes = PrayTimes()
    prayTimes.timeFormat = prayTimes.time12//
    prayTimes.calcMethod = prayTimes.makkah// Muslim World League (MWL)
    prayTimes.asrJuristic = prayTimes.hanafi// Shafii (standard)
    prayTimes.adjustHighLats = prayTimes.angleBased
    val offsets = intArrayOf(0, 0, 0, 0, 0, 0, 0) // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}
    prayTimes.tune(offsets)
    val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId))
    cal.time = Date()
    val times = prayTimes.getPrayerTimes(cal, latitude, longitude, timezonoffset)
    println("prayer times for Alowiqila")
    System.out.println("Fajr : " + times.get(0))
    System.out.println("Sunrise : " + times.get(1))
    System.out.println("Duhr : " + times.get(2))
    System.out.println("Asr : " + times.get(3))
    System.out.println("Sunset : " + times.get(4))
    System.out.println("Magrib : " + times.get(5))
    System.out.println("Isha : " + times.get(6))


   // I want the Alarm to be set to the code above it

    fun startAlarm(isNotification:Boolean, isRepeat:Boolean) {
        val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val myIntent= Intent(this@MainActivity, AlarmReceiver::class.java)
        val pendingIntent:PendingIntent
        // SET TIME HERE
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 15)
        calendar.set(Calendar.MINUTE, 20)
        pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0)
        if (!isRepeat)
            manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent)
        else
            manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)
    }

AlarmReceiver.kt

这是我的女巫的警报接收器,我已经向我添加了通知

class AlarmReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
    val builder = NotificationCompat.Builder(context)

    val myIntent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        context,
        0,
        myIntent,
        FLAG_ONE_SHOT
    )

    builder.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("موعد الاذان")
        .setContentIntent(pendingIntent)
        .setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_SOUND)
        .setContentInfo("Info")

    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(1, builder.build())
}

1 个答案:

答案 0 :(得分:0)

您必须将不同的requestCode放入挂起的意图中,每个警报管理器都需要不同的requestCode。 在您的mainActivity中

Builder.load_string('''
<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size
<RV>:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: .3, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True
        touch_multiselect: True

<FloatLayout>:
    size_hint: .7, 1
    pos_hint: {"right": .3, "top": 1}

    Button:
        text: "test"

''')


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [category for category in categories]


class TestApp(App):
    def build(self):
        return RV()

if __name__ == '__main__':
    TestApp().run()