Android Question StartServiceAT problems

db0070

Active Member
Licensed User
Longtime User
In a service module I start MediaPlayer activity that loads a layout. In MediaPlayer the user interacts and chooses the next alarm time. How can I make sure that MediaPlayer completes (i.e. the user selects his alarm time and presses Done) before it reaches my StartServiceAt line in the service module? I have tried CallSubDelayed2.

This code is in Sub Service_Create, and I find it has reached the 'Log' line well before the MediaPlayer activity has initialised, let alone allow the user the time to make his selections:-
B4X:
    StartActivity(MediaPlayer) 
    StartServiceAt(Me, NextTimeInstance(DateTime.GetHour(MediaPlayer.t), DateTime.GetMinute(MediaPlayer.t)), True)
    Log("MediaPlayer stopped")
 
Last edited:

db0070

Active Member
Licensed User
Longtime User
I have this issue which I cannot debug, because when I am in debug mode, connected to my Samsung iPad the code works. But when I install it in release mode then I get the message saying that unfortunately the application has stopped.

This is is in my Service module (called 'alarms'):-
B4X:
Sub Service_Start (StartingIntent As Intent)
    StartActivity(MediaPlayer)
End Sub

and this is in MediaPlayer activity:-

B4X:
Sub Stop_Click
....
    Dim ms As String
    ms = "No Alarms were set"
    If i > -1 Then
        t  = FindNextTime(tAlarms)
        CallSubDelayed (Me,SetExactAndAllowWhileIdle(t,"alarms"))
        ms = "Next Alarm set at " & DateTime.Time(t)
    End If
    ToastMessageShow(ms, True)
....
End Sub

Sub SetExactAndAllowWhileIdle (Time As Long, ServiceName As String)
    Dim p As Phone
    If p.SdkVersion < 23 Then
        StartServiceAtExact(ServiceName, Time, True)
    Else
        Dim in As Intent
        in.Initialize("", "")
        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
        Dim jin As JavaObject = in
        jin.RunMethod("setAction", Array(Null))
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
        Dim pi As JavaObject
        pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getService", _
  Array(ctxt, 1, in, 134217728))
        am.RunMethod("setExactAndAllowWhileIdle", Array(0, Time, pi))
    End If
End Sub
 
Last edited:
Upvote 0

db0070

Active Member
Licensed User
Longtime User
I get the error:

java.lang.Exception: Sub was not found.

after the CallSubDelayed (Me,SetExactAndAllowWhileIdle(t,"alarm")) is executed. In debug mode it carries on, release mode I guess this is what is making it crash. How do I get rid of this java.lang.Exception error?
 
Upvote 0
Top