Android Question Two Activities with shake

AlpVir

Well-Known Member
Licensed User
Longtime User
The attached project is a variant of "Two Activities Example".
The second task is started shaking the smartphone; not by pressing a button !!!

It works perfectly, BUT :
- if you press the power button
- if you fall on the screen timeout
no longer able to resurrect the service.
Probably it is a problem that can be overcome with the PartialLock and/or KeepAlive instructions but do not know where to place them.
Someone can help me to complete this (strange) project?
Thank you in advance.

B4X:
MAIN :

'   Manifest         :   AddReplacement(android.intent.action.Serv, unused_action)
'   Libraries         :   Phone, Audio
'   Set timeout display to 30"

Sub Process_Globals
End Sub

Sub Globals
    Dim BotStartService           As Button
    Dim Phone                        As PhoneWakeState       
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.Title="MAIN"
    Dim Lb    As Label
    Lb.Initialize("")
    Activity.AddView(Lb,2dip,10dip,100%x-4dip,100dip)
    Lb.Text ="2 Activity (""Main"" and ""Two"") and 1 service (""Serv"")." & CRLF & CRLF & "To start activity ""Two"" press the START button." & CRLF & "When you want to start the activity ""Two"" shake the smartphone."
  
    BotStartService.Initialize("BotStartService")
    Activity.AddView(BotStartService,2dip,150dip,340dip,50dip)
    BotStartService.Text="START ACTIVITY WITH SHAKE"
End Sub

Sub Activity_Resume
    Phone.PartialLock
    'Phone.KeepAlive(False)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Phone.ReleasePartialLock
    'Phone.ReleaseKeepAlive
End Sub

Sub BotStartService_click
    StartService(Serv)
    Activity.Finish
End Sub

SERVICE Serv :
B4X:
Sub Process_Globals
    Dim LastX, LastY, LastZ         As Float
    Dim Accelorometer1                 As PhoneAccelerometer
    Dim Notification1                 As Notification
    Dim T                            As Timer
    Dim TotSec                        As Int 
    Dim Phone                        As PhoneWakeState  
    Dim LastX, LastY, LastZ         As Float   
End Sub

Sub Service_Create
    Log("SERVICE CREATE")
    Notification1.Initialize
    Notification1.Icon = "icon"
    Notification1.Vibrate = True
End Sub

Sub Service_Start (StartingIntent As Intent)
    LogColor ("Serv  START",Colors.blue)
    LastX = 0  : LastY = 0  :    LastZ = 0
    Accelorometer1.StartListening("Accelorometer1")
  
    Notification1.SetInfo("StartActivityWithShake", "Shake the smartphone", Main)
    Notification1.Sound = False
    Service.StartForeground(1, Notification1)
  
    If Not (T.IsInitialized) Then T.Initialize("T",1000)
    T.Enabled=True
    TotSec=0
  
    Phone.PartialLock
    'Phone.KeepAlive(True)
End Sub

Sub Service_TaskRemoved
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy
    Log ("Serv  DESTROY")
    'Phone.ReleasePartialLock
    'Phone.ReleaseKeepAlive
End Sub

Sub Accelorometer1_AccelerometerChanged(x As Float, y As Float, z As Float)
    Dim deltaX, deltaY, deltaZ     As Float
    Dim Vector                     As Float  
    Dim Sensib                    As Float=20
    deltaX = Abs(LastX - X)
    deltaY = Abs(LastY - Y)
    deltaZ = Abs(LastZ - Z)
  
    If (deltaX < 0) Then deltaX = 0
    If (deltaY < 0) Then deltaY = 0
    If (deltaZ < 0) Then deltaZ = 0
    LastX = X  : LastY = Y  :    LastZ = Z

    If (deltaX+deltaY+deltaX)> 0.5 Then
        Vector=Sqrt(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ)
        If Vector>Sensib Then
            Accelorometer1.StopListening
            LastX=0 : LastY =0: LastZ=0
            Log ("SHAKE  " & NumberFormat(Vector,3,1) & " / " & Sensib)
            StartActivity(Two)
        Else
            Log ("VECTOR=" & NumberFormat(Vector,3,1) & " < " & Sensib)      
        End If
    End If
End Sub

Sub T_tick
    TotSec=TotSec+1
End Sub

ACTIVITY TWO :

B4X:
Sub Process_Globals
    Dim N        As Int
    Dim TimerClose            As Timer
End Sub

Sub Globals
    Dim BotEnd                As Button
    Dim LbN                    As Label
    Dim LbCountDown            As Label
    Dim TickClose            As Int
    Dim Phone                        As PhoneWakeState  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.Title="SECOND ACTIVITY"
    N=N+1
    LbN.Initialize("LbN")
    Activity.AddView(LbN,30dip,50dip,300dip,60dip)
    LbN.Text = "Shakings number : " & N :    LbN.TextSize=24
  
    LbCountDown.Initialize("LbCountDown")
    Activity.AddView(LbCountDown,30dip,100dip,100dip,60dip)
    BotEnd.Initialize("BotEnd")
    Activity.AddView(BotEnd,10dip,200dip,200dip,50dip) : BotEnd.Text="END"
  
    If TimerClose.IsInitialized=False Then
        TimerClose.Initialize("TimerClose",1000)
    End If
    TimerClose.Enabled=True
    TickClose=3 ' 10
End Sub

Sub Activity_Resume
    SetShowWhenLocked
    Phone.PartialLock
    Phone.KeepAlive(False)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Phone.ReleasePartialLock
    Phone.ReleaseKeepAlive
End Sub

Sub BotEnd_click
    StopService(Serv)
    ExitApplication
End Sub

Sub TimerClose_tick
    Dim Suono As Beeper
    Suono.Initialize(500,1000)
    Suono.Beep
    '
    TickClose=TickClose-1
    LbCountDown.text="CountDown : -" & TickClose
    If TickClose=0 Then
        TimerClose.Enabled = False
        ToastMessageShow("From the beginning ...",False)
        Activity.Finish
        StartService(Serv)
    End If
End Sub

Sub SetShowWhenLocked
   Dim r As Reflector
   r.Target = r.GetActivity
   r.Target = r.RunMethod("getWindow")
   r.RunMethod2("addFlags", 6815872, "java.lang.int")
End Sub
 

Attachments

  • TwoActivitiesWhithShake.zip
    5.8 KB · Views: 344
Cookies are required to use this site. You must accept them to continue using the site. Learn more…