Android Question High battery usage Application ?

Discussion in 'Android Questions' started by Waldemar Lima, Feb 14, 2018.

  1. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    hi everyone !
    how can I decrease the usage of my battery application?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    What is your app doing?
     
  3. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    its a bus tracker , using gps , google maps and other things
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Does it continue to use the GPS when it is in the background?
     
  5. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    yes , but how can i inactive ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You should provide more information. We cannot guess what your app is doing.
     
  7. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    hi !

    this are all resurces what my application using ...

    Code:
    Sub Process_Globals

        
    Public server As Socket ' connect to tcp server
        Public AStreams As AsyncStreams
        
    Dim ApplicationPass As Int = 0
        
    Dim ConnectTimeOut As Int = 0
        
    Dim ApplicationCode As String = ""
        
    Public GPS1 As GPS ' verify if GPS is enabled
        
    End Sub

    Sub Globals
        
        
    'Gmaps Screen
        Private gmap As GoogleMap ' using gmaps
        Private MapFragment1 As MapFragment
        
    Private Button1 As Button
        
    Private Button1_n As Button
        
    Private Button2_n As Button
        
    Private Button3_n As Button
        
    Private Button4_n As Button
        
    Private bmp As Bitmap
        
    Private cp As CameraPosition
        
    Private MenuIsVisible As Int
        
    'Main Screen
        Private ImageView1 As ImageView
        
    Private Panel1 As Panel
        
    Private Button1_Acess As Button
        
    Private Label1_acesscode As Label
        
    Private EditText1_acesscode As EditText
        
    End Sub
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    1. GPS, Socket and AsyncStreams should never be implemented in the activity code. Only in a service.

    2. If you want to preserve battery then stop the GPS when the activity is paused.
     
  9. Peter Simpson

    Peter Simpson Expert Licensed User

    You should look at FusedLocationProvider
     
    Last edited: Feb 18, 2018
  10. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    well , still high battery usage :c

    Main code =
    Code:
    #Region  Project Attributes
       
    #ApplicationLabel: Bus Tracker
       
    #VersionCode: 1
       
    #VersionName:
       
    'SupportedOrientations possible values: unspecified, landscape or portrait.
       #SupportedOrientations: unspecified
       
    #CanInstallToExternalStorage: False
        
    #AdditionalJar: com.google.android.gms:play-services-maps

    #End Region

    #Region  Activity Attributes
       
    #FullScreen: False
       
    #IncludeTitle: False
    #End Region

    'Activity module
    Sub Process_Globals

    End Sub

    Sub Globals
        
    Private gmap As GoogleMap
        
    Private MapFragment1 As MapFragment
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("1")
        
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
            
    ToastMessageShow("Please install Google Play Services."True)
        
    End If
        
    End Sub

    Sub MapFragment1_Ready
        gmap = MapFragment1.GetMap
        
    Dim m1 As Marker = gmap.AddMarker(1030"test")
        m1.Snippet = 
    "This is the snippet"
    End Sub

    Sub Activity_Resume
        Starter.GPS1.Start(
    0,0)
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        Starter.GPS1.Stop
    End Sub

    Starter code :

    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
        
    #ExcludeFromLibrary: True
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Public server As Socket
        
    Public AStreams As AsyncStreams
        
    Public GPS1 As GPS
        
    End Sub

    Sub Service_Create
        
        GPS1.Initialize(
    "GPS1")
        
    server.Initialize("server")
        
    server.Connect("192.168.1.4",5562,5000)

    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    If (GPS1.IsInitialized = TrueThen
            
    If GPS1.GPSEnabled = False Then
                
    ToastMessageShow("Por favor , ative o GPS !"True)
                
    StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
            End If
        
    Else
            
    Msgbox("Não foi possível inicializar o GPS .","Erro ao iniciar GPS.")
            
    ExitApplication
        
    End If
    End Sub

    Sub Service_TaskRemoved
        
    'This event will be raised when the user removes the app from the recent apps list.
    End Sub

    'Return true to allow the OS default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
        
    Return True
    End Sub

    Sub Service_Destroy

    End Sub

    Sub server_Connected (Successful As Boolean)
        
    If (Successful = True ) Then
            
    Log("Connectado !")
        
    End If
    End Sub

    What is causing the high usage of the battery is the GPS?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    1. You cannot show Msgbox from a service.
    2. You should never call ExitApplication.

    The GPS can be the cause of the high battery usage.
     
  12. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    thsnka by help Erel :D
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice