Android Question How to make sensor listen while screen off

Albert Lin

Member
Licensed User
Longtime User
Dear All
When the screen off , the senor seems to stop listening. How to make it listen while screen off.
Can anyone give me some suggestion? please.
Thanks very much in advance. My code is as following.

Main
=================================
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
StartServiceAt(svWakeup, DateTime.Now , True)
sensor.Initialize(sensor.TYPE_ACCELEROMETER)

End If
End Sub

Sub Activity_Resume
sensor.StartListening("sensor")
End Sub

Sub RestartSensor
sensor.StopListening
sensor.StartListening("sensor")
sensor.StartListening("sensor")
End Sub

Sub sensor_SensorChanged(Values() As Float)
Shake.HandleSensorEvent(Values)
End Sub
'---------------------------------------------------------------------------------------
svWakeup
==================================================
#Region Service Attributes
#StartAtBoot: False
#End Region

Sub Process_Globals
Dim PhoneId As PhoneId
Dim PE As PhoneEvents

End Sub
Sub Service_Create
PE.InitializeWithPhoneState("PE", PhoneId)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
Sub PE_ScreenOff (Intent As Intent)
CallSub(Main,"RestartSensor")
End Sub

Sub PE_ScreenOn (Intent As Intent)
ToastMessageShow("Screen is ON",False)
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Please use code Tags when posting code!
Edit your post and add the cod-tags.

codetag001.png


codetag002.png


codetag003.png
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
You should move all of your sensor code into a service. This will allow you to still monitor the sensor when the screen is off.
 
Upvote 0

Albert Lin

Member
Licensed User
Longtime User
Please use code Tags when posting code!
Edit your post and add the cod-tags.

codetag001.png


codetag002.png


codetag003.png
Dear Fixit30

I did what you said to move all of my sensor code into a service, but it still didn't work.
Is there something that I missed?

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim PhoneId As PhoneId
    Dim PE As PhoneEvents
    Dim sensor As PhoneSensors
    Dim timer2 As Timer
    Dim gVibrate As PhoneVibrate
End Sub
Sub Service_Create
    PE.InitializeWithPhoneState("PE", PhoneId)
    sensor.Initialize(sensor.TYPE_ACCELEROMETER)
    Shake.CallBackActivity = "svWakeup" ' 指定處理搖晃事件的活動
    timer2.Initialize("Timer2", 500)
End Sub

Sub Service_Start (StartingIntent As Intent)
    sensor.StartListening("sensor")
End Sub

Sub Service_Destroy

End Sub
Sub PE_ScreenOff (Intent As Intent)
    'ToastMessageShow("Screen is OFF",True)
    sensor.StopListening
    Common.Sleep(500)
    sensor.StartListening("sensor")
    sensor.StartListening("sensor")
End Sub

Sub PE_ScreenOn (Intent As Intent)
    ToastMessageShow("Screen is ON",False)
   
End Sub
Sub sensor_SensorChanged(Values() As Float)
    Shake.HandleSensorEvent(Values)
End Sub
Sub ShakeEvent
Dim s0,s1 As String

    gVibrate.Vibrate (390)
    timer2.Enabled =True
    'btnSave_Click 在此直接run會當機
'    Activity.Color=Colors.Black
'    chkLongMode.Checked =True
'    clv.Show (False)
End Sub
Sub Timer2_Tick
    timer2.Enabled =False
    CallSub(Main, "btnSave_Click")
End Sub
 
Upvote 0

Albert Lin

Member
Licensed User
Longtime User
最好能贴出来完整代码,您说的不运行是什么情况?

wy328, post: 456947, member: 20985"]最好能贴出来完整代码,您说的不运行是什么情况?[/QUOTE]

The whole code is as attachment. Shaking the phone is able to increade <Save Time> while screen on, but there is no action for shaking phone while screen off. please be kind to do me a favor what's wrong with it. How can I make sensor work while screen off
 

Attachments

  • _Shake_Test.zip
    6.1 KB · Views: 223
Upvote 0

wy328

Member
Licensed User
Longtime User
wy328, post: 456947, member: 20985"]最好能贴出来完整代码,您说的不运行是什么情况?

The whole code is as attachment. Shaking the phone is able to increade <Save Time> while screen on, but there is no action for shaking phone while screen off. please be kind to do me a favor what's wrong with it. How can I make sensor work while screen off[/QUOTE]

I'll try this code later. :p
 
Upvote 0

Albert Lin

Member
Licensed User
Longtime User
The whole code is as attachment. Shaking the phone is able to increade <Save Time> while screen on, but there is no action for shaking phone while screen off. please be kind to do me a favor what's wrong with it. How can I make sensor work while screen off

I'll try this code later. :p[/QUOTE]
Thank you very much
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
This is an unsolved problem to me too, I made an app working with sensors and it works fine in sleep mode with Galaxy S3 Neo (Android 4.4) but not with an old Galaxy Next (Android 4.1.2), I tried with PartialLock but it's always the same, the Galaxy Next have not any power saving setted... I still working around... any suggestion?
Thank you
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
Have you already tried to restart the Sensor listening when the screen goes off ?
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
Are you using a foreground service with a partial lock?
Yes! Just done... but nothing... phone seems dead when the screen goes off, even the debugger does not receive any information, as I wrote before I'v no problem with Galaxy S3 NEO, I don't need partial lock or foreground, but when I tested my app in the Galaxy Next I relized that it doesn't works in sleep mode...
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
Worth testing in Release mode.

It is possible that your device prevents all processes from running when the screen turns off to save battery.
Sure you're right Erel, but I can't find any setting to avoid this in the phone.
P.S. Indeed only the sensor are stopped, if I install a dummy service that just make a beep it works in sleep mode...
ERRATA CORRIGE: The not working phone is a Galaxy ACE and not NEXT as I wrote before.
 
Last edited:
Upvote 0

Sgardy

Member
Licensed User
Longtime User
At last I think that this it's impossible to obtain in some phones, I tried to install apps such anti theft, they works in Galaxy S3 bu not in Galaxy ACE so I give up!
 
Upvote 0
Top