Android Question Bypassing "Blocking Mode" on phones

gawie007

Member
Licensed User
Longtime User
Background:
This is used for our GSM Module which monitors industrial conditions (alarms etc.). A SMS will be sent to the user when an event is raised. The notification on the phone must be raised, be audible and vibrate irrespective of the settings on the phone.
This code works perfectly until I enable "Blocking Mode" on my Samsung Note 2 phone. The phone blocks any audible or vibration raised by a notification. see: http://eguides.sprint.com/support/e...sung_galaxy_note_ii_eguide/blocking_mode.html
I could change the Blocking Mode settings to allow the mobile number to be exempt from the Blocking Mode rules, but I have to consider other phones that may have similar features. Therefore, I have to find a way to override these settings.

I have been searching for a few days now to find what setting "Blocking Mode" switches on or off and don't even know where or how to change it.
Just a nudge in the right direction will be appreciated!

I have also attached the following code in the file below.

Main:
B4X:
#Region  Project Attributes
    #ApplicationLabel: IMF SMS Interceptor
    #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
 
    Dim textAddress As String
    Dim textBody As String
 
End Sub
 
Sub Globals
 
    Dim lblAddress As Label
    Dim lblBody As Label
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
 
    Activity.LoadLayout("main")
 
End Sub
 
Sub Activity_Resume
 
    lblAddress.Text = textAddress
    lblBody.Text = textBody
 
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

s1:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region
 
'Service module
Sub Process_Globals
  Type Message (Address As String, Body As String)
  Dim currentAddress As String
  Dim currentBody As String
 
  Dim p As Phone
  Dim OriginalRingerMode As  Int
  Dim OriginalVOLUME_NOTIFICATION As Int
 
End Sub
Sub Service_Create
 
End Sub
 
Sub Service_Start(startingIntent As Intent)
  If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
      Dim messages() As Message
      messages = ParseSmsIntent(startingIntent)
      For i = 0 To messages.Length - 1
        Log(messages(i))
        If messages(i).Address = "+447507878xxx" Then
        currentAddress = messages(i).Address
        currentBody = messages(i).Body
            raiseAlarm
        End If
      Next
  End If
End Sub
 
Sub raiseAlarm
'    Dim pv As PhoneVibrate
'    pv.Vibrate(500)    'This runs irrespective of RingerMode etc
 
    SetRingerMode
 
    SetVOLUME_NOTIFICATIONtoMaximum
 
    RaiseNotification
 
    ReinstateVOLUME_NOTIFICATION
 
    ReinstateRingerMode
 
End Sub
 
Sub SetRingerMode
 
'    Dim RingerMode As Int
'    Dim Target As Object
'    Dim r As Reflector
'
'    r.Target = "android.media.AudioManager"
'    Log("r.Target: " & r.Target)
'    RingerMode = r.RunMethod("getRingerMode")
'    'RingerMode = r.RunStaticMethod("android.media.AudioManager","getRingerMode",Null,Null)
'    Log("RingerMode in CheckNotificationSetting: " & RingerMode)
 
'--------------------------------------------------------------------------------------------
    'RINGER_MODE_SILENT = 0
    'RINGER_MODE_VIBRATE = 1
    'RINGER_MODE_NORMAL = 2 Audible & Vibrate
 
 
    Log("Ringer Mode: " & p.GetRingerMode)
    OriginalRingerMode = p.GetRingerMode    'Global variable to reinstate original settings
 
    If p.GetRingerMode <> 2 Then
        p.SetRingerMode(p.RINGER_NORMAL)
    End If
 
End Sub
 
Sub SetVOLUME_NOTIFICATIONtoMaximum
 
    Log("Volume Notification: " & p.GetVolume(p.VOLUME_NOTIFICATION))
    OriginalVOLUME_NOTIFICATION = p.GetVolume(p.VOLUME_NOTIFICATION)    'Get the volume before setting max volume
 
    Dim maxVolume As Int
    maxVolume = p.GetMaxVolume(p.VOLUME_NOTIFICATION)
    p.SetVolume(p.VOLUME_NOTIFICATION,maxVolume,False)    'Set Max Volume
    Log("Notification MaxVolume: " & maxVolume)
 
End Sub
 
Sub RaiseNotification
    Dim n As Notification
    n.Initialize
    n.Light = True
    n.Insistent = True
    n.Sound = True
    n.Icon = "icon"
    n.SetInfo("IMF GSM 1301 Message", ("From " & currentAddress),Main)
    n.Notify(1)
 
    Main.textAddress = currentAddress
    Main.textBody = currentBody
 
End Sub
 
Sub ReinstateVOLUME_NOTIFICATION
 
    Log("Temporary Volume Notification: " & p.GetVolume(p.VOLUME_NOTIFICATION))
    p.SetVolume(p.VOLUME_NOTIFICATION,OriginalVOLUME_NOTIFICATION,False)    'Return the volume to its original volume
    Log("Reinstated Volume Notification: " & p.GetVolume(p.VOLUME_NOTIFICATION))
 
End Sub
 
Sub ReinstateRingerMode
 
    Log("Temporary Ringer Mode: " & p.GetRingerMode)
    p.SetRingerMode(OriginalRingerMode)
    Log("Reinstated Ringer Mode: " & p.GetRingerMode)
 
End Sub
 
'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As Message()
  Dim messages() As Message
  If In.HasExtra("pdus") = False Then Return messages
  Dim pdus() As Object
  Dim r As Reflector
  pdus = In.GetExtra("pdus")
  If pdus.Length > 0 Then
      Dim messages(pdus.Length) As Message
      For i = 0 To pdus.Length - 1
        r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
        messages(i).Body = r.RunMethod("getMessageBody")
        messages(i).Address = r.RunMethod("getOriginatingAddress")
      Next
  End If
  Return messages
End Sub



In order to test it, I was sending an SMS back to myself. The number ending with xxx in s1 would need to reflect the user's phone number.

By the way, this is brilliant the way that Erel has written the basis for all this (Intent Filters). Even if I Force Close my application, it starts up again as soon as I have an incoming SMS. Pure Genius!
 

Attachments

  • SMSInterceptor.zip
    8.6 KB · Views: 313
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
By the way, this is brilliant the way that Erel has written the basis for all this (Intent Filters). Even if I Force Close my application, it starts up again as soon as I have an incoming SMS. Pure Genius!
The credit is not mine... It is an Android feature.

This blocking mode is specific to Samsung. You should search for a Java solution. If such exists then it should be possible to port it.

You can also use MediaPlayer to play sounds. Even if the notification sounds are disabled.
 
Upvote 0

gawie007

Member
Licensed User
Longtime User
Hi Erel,

I understand it is a feature but before this I only thought you could run services or applications either by events or start-up. Through your knowledge, I now know that I can run applications on demand whether they are currently running or not. I suppose I could have rephrased it.

There are a couple of other phones with this blocking mode and then there are others that utilise Apps to block notifications / callers etc.

I suppose using the MediaPlayer will make more sense then. It will then also not be necessary to fiddle with other settings and allow a bespoke sound.

Thank you for your help.
 
Upvote 0
Top