Android Question Setting alarm days of week programatically in the stock Android alarm app

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I would like to programatically set in the stock Android alarm app the days of the week an alarm will be set. I found a reference to EXTRA_DAYS from this link: https://developer.android.com/reference/android/provider/AlarmClock#EXTRA_DAYS

Unfortunately, that web site is for people who know Java which I don't know. Anyway, I tried to create an array and guess what needed to be loaded in the array to pass to EXTRA_DAYS so I could set an alarm to go off on Monday, Wednesday and Friday just to see if I got the coding right. I got no errors, but the stock Android alarm app displayed a toast message that the alarm was set to go off today at the time set in my coding. The days of the week for that particular alarm were not set (they were dimmed out). This tells me my guess at coding the days of the week is wrong.

Can you tell me what I need to pass to EXTRA_DAYS so the correct days will be set? All I know so far is that it does need Int numbers in the array.

This is also the first time trying out arrays.

Here's the coding.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private i As Intent
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")

    Dim arDays() As Int = Array As Int(1, 3, 5)
    
    i.Initialize("android.intent.action.SET_ALARM", "")
    i.PutExtra("android.intent.extra.alarm.HOUR", 20)
    i.PutExtra("android.intent.extra.alarm.MINUTES", 30)
    i.PutExtra("android.intent.extra.alarm.MESSAGE","My Alarm")
    i.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
    i.PutExtra("android.intent.extra.alarm.DAYS", arDays)
    StartActivity(i)
End Sub

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Why is i not a local variable? This is a mistake.

I've tried your code and it works correctly.
You do need to add the permission:
B4X:
AddPermission(com.android.alarm.permission.SET_ALARM)
Greetings Erel,

I made the changes to the intent declaration. The permission was already added to the manifest file and I'm able to only set an alarm for the current day. If you look at the attachment, the days of the week are displayed for "my alarm" but they are not selected like the other alarms I added myself not from the B4A app.

Here is all my coding that sets the alarm. Can you check my array setup and let me know if I have it loaded with the correct numbers for setting the alarm for Monday, Wednesday and Friday?

Thanks.

Manifest file:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

AddPermission(com.android.alarm.permission.SET_ALARM)

AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="28" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)

CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

Main Module:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")

    Dim arDays() As Int = Array As Int(1, 3, 5)
    Dim i As Intent
   
'    Dim FileName As String = "mantle_one_chime.mp3"
'    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
'    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
   
'    Log("The file URI is: " & Starter.Provider.GetFileUri(FileName))

    i.Initialize("android.intent.action.SET_ALARM", "")
    i.PutExtra("android.intent.extra.alarm.HOUR", 20)
    i.PutExtra("android.intent.extra.alarm.MINUTES", 30)
    i.PutExtra("android.intent.extra.alarm.MESSAGE","My Alarm")
    i.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
    i.PutExtra("android.intent.extra.alarm.DAYS", arDays)
'    i.PutExtra("android.intent.extra.alarm.RINGTONE", _
'        Starter.Provider.GetFileUri(strFileUri))
       
    StartActivity(i)
End Sub

Starter:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Public Provider As FileProvider
End Sub
WhatsApp Image 2020-03-24 at 13.49.01.jpeg
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
looking at the documentation, i think your days should be 2,4,6 not 1,3,5
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
looking at the documentation, i think your days should be 2,4,6 not 1,3,5
Hi,

Just tried it but the alarm days of the week are still dimmed out (unselected) for those days.

I also noticed the alarm app displays a toast message indicating the alarm is set to go off in 1 minute even though the time was 20:35 and the alarm coding shows 20:30. Maybe it's a problem with the alarm app? I'm running Android version 10 on a Samsung S9 phone if that helps.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Yeah, I was just going to ask you that...

Were you able to find any other app that can use an intent to set the "day" of the built-in alarm app?

For example, can you say "OK Google.....set alarm for Friday at 2pm". If the built in google app can't even set the days, then maybe your alarm app was not written to fully support that parameter of the intent.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Yeah, I was just going to ask you that...

Were you able to find any other app that can use an intent to set the "day" of the built-in alarm app?

For example, can you say "OK Google.....set alarm for Friday at 2pm". If the built in google app can't even set the days, then maybe your alarm app was not written to fully support that parameter of the intent.
Hi John,

Ok Google can set the days of the week for the alarm so it looks like a problem with my app. Tomorrow I will upload the app in hopes someone can help in finding out what coding I left out.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your code works for me. i set alarm for mon,wed,fri (2,4,6) at 0830H (see attached). note: when the intent fires, there is a quick toast-like message back to the calling app. i couldn't quite catch it, but what little i saw made me think the alarm set had failed. but when i look at the default clock app, what you see is what i got. i ran the test on nexus 5x, os8.1 min 19, target 28
 

Attachments

  • rleiman.png
    rleiman.png
    16.3 KB · Views: 404
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
your code works for me. i set alarm for mon,wed,fri (2,4,6) at 0830H (see attached). note: when the intent fires, there is a quick toast-like message back to the calling app. i couldn't quite catch it, but what little i saw made me think the alarm set had failed. but when i look at the default clock app, what you see is what i got. i ran the test on nexus 5x, os8.1 min 19, target 28
Hi,

Since you were able to run the coding on your phone, can you unzip my project and see if it still works on your phone? I'm hoping to find out if my phone's stock alarm app can't handle the coding. Maybe I'm missing something with my project.

Here's the zipped up app I have on my Google Drive account:


Thanks.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
runs fine on my device. give me $75, and i'll sell you my nexus 5x. comes with all my apps pre-installed. win-win ?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
there seems to be a fair amount of squawking about the samsung default clock. maybe you should start down that path. you might back into some code.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
there seems to be a fair amount of squawking about the samsung default clock. maybe you should start down that path. you might back into some code.
It doesn't surprise me about how people are complaining about that Samsung app.

I originally wrote a clock app with a lot of functions and was going to add alarms to it by calling the stock Android app. Now that I know not all phones support it, I will use StartServiceAt so a service can sound the alarm according to the days of the week the user chooses. If the Samsung app worked, it would have saved me a lot of coding.

I think I may have to start a new thread for this question and hope it's ok to ask in this thread.

Can I call StartServiceAt multiple times for example and use 18:00, 05:00 and 14:30 to start the same service at all of those times?

You win some and loose some. ?
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
my guess would be yes, but someone is bound to recommend you start a new thread, so i'll do it for them: new thread, please
 
Upvote 0
Top