Android Question [SOLVED] - Adding alarms to the stock Android clock app programmatically.

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I would like to know if it's possible to add alarms to the stock Android clock app programmatically.

If this can be done, can you point me to a link that shows what coding is needed? I would like to set alarm parameters such as days when the alarm will play, the sound file which the alarm will play, and all the other parameters that the stock Android alarm app supports. Also I would like to load all of the alarm titles that the user has set up if that's also possible.

If none of this is possible, I will add all of that functionality to my app by hand. Just wanting to take out re-inventing the wheel. ;)
 

kisoft

Well-Known Member
Licensed User
Longtime User
HI
It's probably a similar problem...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for the replies. I used this coding based from https://www.b4x.com/android/forum/threads/setting-alarm-for-a-specific-time.71167/ to try and set an alarm.

B4X:
    i.Initialize("android.intent.action.SET_ALARM", "")
    i.PutExtra("android.intent.extra.alarm.HOUR", 20)
    i.PutExtra("android.intent.extra.alarm.MINUTES", 35)
    i.PutExtra("android.intent.extra.alarm.MESSAGE","My Alarm")
    i.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
    StartActivity(i)

Looks like because the post is from way back in 2016 a new permission is needed to set an alarm with an intent. Since I couldn't locate any tutorial on the forum, I'm assuming something needs to maybe added to the manifest file as well as some more B4A coding.

My log showed this line stating I needed the permission:

B4X:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SET_ALARM flg=0x20000 cmp=com.sec.android.app.clockpackage/.alarm.activity.AlarmCTSHandleActivity (has extras) } from ProcessRecord{6c06530 8690:b4a.example/u0a382} (pid=8690, uid=10382) requires com.android.alarm.permission.SET_ALARM

I tried to see if there was something in the dropdown called PERMISSION_SET_ALARM when coding: rp.PERMISSION_ ... but it wasn't a choice to select.


All help will be appreciated.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Here's the coding for anyone following this thread that also got stuck.

The manifest editor:
'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="26"/>
<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)

In the main module:
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")

    i.Initialize("android.intent.action.SET_ALARM", "")
    i.PutExtra("android.intent.extra.alarm.HOUR", 20)
    i.PutExtra("android.intent.extra.alarm.MINUTES", 35)
    i.PutExtra("android.intent.extra.alarm.MESSAGE","My Alarm")
    i.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
    StartActivity(i)
End Sub

When you look at your stock alarm app, the alarm will be set and ready to go.
 
Upvote 0
Top