iOS Question detect pressed Power OFF button

Dieter Baumgartner

Member
Licensed User
Longtime User
Music should only play further in the background, when the display was shut off with the POWER OFF button.
It should stop when the HOME Button was pressed. Both actions are doing Application_Inactive and Application_Background.
How can i check, which Button was pressed, to stop playing sound in the background ?
Is there a way to check, if the POWER OFF Button was pressed ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private locked As Boolean
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   Page1.RootPanel.LoadLayout("input")
   NavControl.ShowPage(Page1)
   
   Dim no As NativeObject = Me
   no.RunMethod("addListener", Null)
End Sub

Sub Application_Foreground
   locked = False
End Sub

Private Sub Application_Background
   Log($"locked: ${locked}"$)
End Sub

#if objc

static void displayStatusChanged(CFNotificationCenterRef center,
  void *observer,
  CFStringRef name,
  const void *object,
  CFDictionaryRef userInfo) {
  if (name == CFSTR("com.apple.springboard.lockcomplete")) {
     [b4i_main new]._locked = true;
  }
}
- (void)addListener {
    // Override point for customization after application launch.
  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  NULL,
  displayStatusChanged,
  CFSTR("com.apple.springboard.lockcomplete"),
  NULL,
  CFNotificationSuspensionBehaviorDeliverImmediately);
}

#end if

It is based on this answer: http://stackoverflow.com/questions/...d-home-button-press-on-ios7/19771680#19771680
Note that it uses undocumented notifications so it may not be future proof.
 
Upvote 0
Top