Hello folks. I need your help. I want to set up an alarm service using the Android clock/alarm.
Setting up an alarm is no problem.
But how do I clear the alarm back in code?
I did some reading in Java/Android and found a method using the AlarmManager.Cancel with the PendingIntet.
As can be seen in the code, I tried to implement this with the Reflection Library and JavaObject.
But no alarm is deleted. What am I doing wrong or what have I not understood?
And further ....
In the test code, I access the currently created intent to create a pending intent.
But how can I access an intent when accessing an already set up alarm?
Here I would have to set up the Uri or the deep link and access them.
That's my idea, but how to realize it?
I've been stuck with this problem for a week and can't get any further.
I am thankful for every help.
And if the answer comes "learn Java", then please say what exactly.
I need your help.
In the manifest i have addet the permissions:
AddPermission(com.android.alarm.permission.SET_ALARM)
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
Here my Code from a testproject
(sorry for my bad english, i'm german)
Here a Java Code Example for cancel a alarm.
Setting up an alarm is no problem.
But how do I clear the alarm back in code?
I did some reading in Java/Android and found a method using the AlarmManager.Cancel with the PendingIntet.
As can be seen in the code, I tried to implement this with the Reflection Library and JavaObject.
But no alarm is deleted. What am I doing wrong or what have I not understood?
And further ....
In the test code, I access the currently created intent to create a pending intent.
But how can I access an intent when accessing an already set up alarm?
Here I would have to set up the Uri or the deep link and access them.
That's my idea, but how to realize it?
I've been stuck with this problem for a week and can't get any further.
I am thankful for every help.
And if the answer comes "learn Java", then please say what exactly.
I need your help.
In the manifest i have addet the permissions:
AddPermission(com.android.alarm.permission.SET_ALARM)
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
Here my Code from a testproject
(sorry for my bad english, i'm german)
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private BasePanel As Panel
Private STCmd, StartCmd, DismissCmd,CancelCmd As Button
Private TLabel As Label
Private AI As Intent
End Sub
Sub Activity_Create(FirstTime As Boolean)
CreateScreen
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Private Sub CreateScreen
BasePanel.Initialize("basepanel")
Activity.AddView(BasePanel,0,0,Activity.Width,Activity.Height)
'++++++++
STCmd.Initialize("stcmd")
BasePanel.AddView(STCmd,5dip,5dip,100dip,40dip)
STCmd.Text="Set Time"
'++++++++
TLabel.Initialize("tlabel")
BasePanel.AddView(TLabel,5dip,50dip,100dip,40dip)
TLabel.Color=Colors.LightGray
TLabel.TextColor=Colors.Black
'++++++++
StartCmd.Initialize("startcmd")
BasePanel.AddView(StartCmd,5dip,100dip,100dip,40dip)
StartCmd.Text="Set Alarm"
'++++++++
DismissCmd.Initialize("dismisscmd")
BasePanel.AddView(DismissCmd,StartCmd.left+StartCmd.Width+5dip,StartCmd.top,100dip,40dip)
DismissCmd.SingleLine=True
DismissCmd.Text="Dismiss Alarm"
'++++++++
CancelCmd.Initialize("cancelcmd")
BasePanel.AddView(CancelCmd,DismissCmd.left+DismissCmd.Width+5dip,DismissCmd.top,100dip,40dip)
CancelCmd.SingleLine=True
CancelCmd.Text="Cancel Alarm"
End Sub
private Sub StartAlarm(hour As Int, minutes As Int,text As String)
Dim AlarmIntent As Intent
Dim arDays() As Int = Array As Int(1,2,3,4,5,6,7)
AlarmIntent.Initialize("android.intent.action.SET_ALARM","")
'Here I would have to set the Uri/Deeplink to select a specific alarm. But I have no idea how to do that.
AI=AlarmIntent ' Current alarm Is noted And can Then be used For testing in "CancelAlarm".
AlarmIntent.PutExtra("android.intent.extra.alarm.HOUR", hour)
AlarmIntent.PutExtra("android.intent.extra.alarm.MINUTES", minutes)
AlarmIntent.PutExtra("android.intent.extra.alarm.MESSAGE",text)
AlarmIntent.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
AlarmIntent.PutExtra("android.intent.extra.alarm.DAYS", arDays)
AlarmIntent.PutExtra("android.intent.extra.alarm.RINGTONE","sound.wav")
StartActivity(AlarmIntent)
End Sub
Private Sub DismissAlarm
Dim AlarmIntent As Intent
AlarmIntent.Initialize("android.intent.action.DISMISS_ALARM","")
StartActivity(AlarmIntent)
End Sub
Private Sub CancelAlarm
Dim rf As Reflector
Dim AlarmManager,PendingIntent,Context As JavaObject
Context=rf.GetContext
AlarmManager.InitializeStatic("android.app.AlarmManager") ' = New AlarmManager
AlarmManager=Context.RunMethod("getSystemService",Array As Object("alarm")) ' Calling AlarmServie
Log(AlarmManager)
PendingIntent.InitializeStatic("android.app.PendingIntent") ' = New PendingIntent
Log(PendingIntent) ' PendingIntent-Class create
Dim Args(4) As Object = Array As Object(Context,0,AI,134217728) ' Number => PendingIntent-Parameter/FLAG_UPDATE_CURRENT
' Dim Types(4) As String = Array As String("android.content.Context","java.lang.Integer","android.content.Intent","java.lang.Integer")
PendingIntent=PendingIntent.RunMethod("getBroadcast",Args) ' Casting PendingIntent Class to PendingIntent Object
Log(PendingIntent) ' PendingIntent-Object Create
AlarmManager.RunMethod("cancel",Array As Object(PendingIntent))
End Sub
Sub startcmd_Click
Dim Time() As String=Regex.Split(":",TLabel.Text)
StartAlarm(Time(0),Time(1),"TestAlarm")
End Sub
Sub cancelcmd_click
CancelAlarm
End Sub
Sub dismisscmd_click
DismissAlarm
End Sub
Sub stcmd_click
Dim td As TimeDialog
Dim Time As String
Dim Hour, Minutes As String
td.Is24Hours=True
td.Show("Select Alarm Time", "", "Ok", "Cancel", "", Null)
If td.Hour < 10 Then Hour="0" & td.Hour Else Hour=td.Hour
If td.Minute < 10 Then Minutes = "0" & td.Minute Else Minutes=td.Minute
Time=Hour & ":" & Minutes
TLabel.Text=Time
End Sub
Here a Java Code Example for cancel a alarm.
B4X:
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Main {
public static void cancel(Context context, Intent intent) {
AlarmManager alarme = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent p = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarme.cancel(p);//ww w .j a va 2s . c o m
}
}
Last edited: