I apologise in advance for asking but having spent hours and hours trying this code and various different variations I really am puzzled as to why this code doesn't work on my phone yet works perfectly on the emulator and so am hoping someone can comment.
I'm really not asking any one to rewrite it or anything like that just that if you see an obvious problem in the code to please enlighten me as to what it is.
I've stripped most irrelevant data out, but left the variable declarations intact in case they are part of the problem somehow.
I believe the cause has something to do with the accelerometer usage because the app works great in the emulator (which has no accelerometer) while on the phone the app will often start up, or resume, thinking it has been shaken and then continue acting as if it has been (by calling the sub named Shaken) until it force closes, usually within seconds of starting.
If the phone is shaken even just once it usually keeps calling sub Shaken.
I have tried everything I can think of even trying to compensate by allowing for a few mistaken shakes with the variable called numberoftimesshaken but it has not helped. I did not write the accelerometer routine myself but the gist of it is from an example that was posted in another thread here and the code itself works so it doesn't seem to be that routine.
Without some advice I think I shall have to omit the shake code altogether which is a shame as the app is a kind of magic 8 ball app where you can shake the phone to shake the ball and get an answer which would be a nice touch if I can keep that feature in use.
I could post the full code if needed but it's a lot longer and doesn't reference the accelerometer, just moves the ball around on screen, generates a number and picks an appropriate response to show within the ball on screen. Plus I don't want to make anyone sift through long lines of unrelated code as I feel quite embarrassed being so stuck I have to ask for assistance with finding the error.
So, and hoping I haven't over stepped any boundaries, can anyone see anything that as a new user I may have done a little wrong that could cause the phone to act as if it has been shaken when it hasn't?
Thanks for any advice.
Dave
Dave
I'm really not asking any one to rewrite it or anything like that just that if you see an obvious problem in the code to please enlighten me as to what it is.
I've stripped most irrelevant data out, but left the variable declarations intact in case they are part of the problem somehow.
I believe the cause has something to do with the accelerometer usage because the app works great in the emulator (which has no accelerometer) while on the phone the app will often start up, or resume, thinking it has been shaken and then continue acting as if it has been (by calling the sub named Shaken) until it force closes, usually within seconds of starting.
If the phone is shaken even just once it usually keeps calling sub Shaken.
I have tried everything I can think of even trying to compensate by allowing for a few mistaken shakes with the variable called numberoftimesshaken but it has not helped. I did not write the accelerometer routine myself but the gist of it is from an example that was posted in another thread here and the code itself works so it doesn't seem to be that routine.
Without some advice I think I shall have to omit the shake code altogether which is a shame as the app is a kind of magic 8 ball app where you can shake the phone to shake the ball and get an answer which would be a nice touch if I can keep that feature in use.
I could post the full code if needed but it's a lot longer and doesn't reference the accelerometer, just moves the ball around on screen, generates a number and picks an appropriate response to show within the ball on screen. Plus I don't want to make anyone sift through long lines of unrelated code as I feel quite embarrassed being so stuck I have to ask for assistance with finding the error.
So, and hoping I haven't over stepped any boundaries, can anyone see anything that as a new user I may have done a little wrong that could cause the phone to act as if it has been shaken when it hasn't?
B4X:
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim MediaPlayer1 As MediaPlayer
Dim l As Int
Dim LastX, LastY, LastZ As Float
Dim LastUpdate, TimeDiff As Long
Dim Activated As Boolean
Activated=True
Dim ShakeThreshold As Double
ShakeThreshold=1200
Dim List1_numerology As List
Dim randomnumber As Int
Dim highest As Int
Dim pws As PhoneWakeState
Dim numberoftimesshaken As Int
Dim Delay As Long
Dim waitover As Long
Dim p As Phone
Dim Jerk As Double
Dim JerkX, JerkYX, JerkZ As Double
Dim MyFont As Typeface
MyFont = Typeface.LoadFromAssets("transist.ttf")
MyFont = Typeface.CreateNew(MyFont, Typeface.STYLE_BOLD)
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Accelorometer As PhoneAccelerometer
Dim ImageView1_ball As ImageView
Dim ImageView1_sky As ImageView
Dim Spinner1 As Spinner
Dim Label1_balltext As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main.bal")
If FirstTime Then
' We add the following value because app is starting and for some reason
' acting as if phone was shaken
' so this means it has to be shook a few times to register
numberoftimesshaken = 0
Accelorometer.StartListening("Accelorometer")
LastUpdate=DateTime.Now
End If
End Sub
Sub Activity_Resume
numberoftimesshaken = 0
LastUpdate=DateTime.Now
pws.keepalive(True)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
pws.ReleaseKeepAlive
End Sub
Sub Label1_balltext_Click
predict
End Sub
Sub predict
ballmove
End Sub
Sub ballmove
p.vibrate(20)
End Sub
Sub Accelorometer_AccelerometerChanged(x As Float, y As Float, z As Float)
If Activated=True Then
TimeDiff=DateTime.Now-LastUpdate
Jerk=Abs(x-LastX+y-LastY+z-LastZ)/TimeDiff*10000
If Jerk>=ShakeThreshold Then numberoftimesshaken = numberoftimesshaken +1
LastX=x
LastY=y
LastZ=z
End If
If numberoftimesshaken >=4 Then Shaked
LastUpdate=DateTime.Now
End Sub
Sub Shaked
predict
ToastMessageShow(numberoftimesshaken, True)
numberoftimesshaken=0
End Sub
Thanks for any advice.
Dave
Dave