Android Question app is consuming all battery time

pesquera

Active Member
Licensed User
Longtime User
Hi everybody,

It's a bit weird: while the app is consuming all the time, the % of use is very low (see the images)
I can't figure it out where is my mistake

B4A v8.00, Android 8.1.0

There are only 3 services: Starter, habilitar_smsread and habilitar_push

The relevant code:
B4X:
Starter
Service_Create
     Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
     If IsPaused(habilitar_push) Then
          StartService(habilitar_push)       
     End If   
     If IsPaused(habilitar_smsread) Then
           StartService(habilitar_smsread)                   
     End If   
Service_Start
     Sleep(0)
     Service.StopAutomaticForeground

habilitar_smsread
Service_Create
     GLO_NotificationSvc.Initialize
     GLO_NotificationSvc.Icon = "icon"
     GLO_NotificationSvc.Sound = False
     GLO_NotificationSvc.SetInfo("Agendame.Turnos.Habilitar","activado!",Main)
     Service.StartForeground(123456788,GLO_NotificationSvc)
Service_Start
     If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
          Dim misSMSList As List
          misSMSList = rtn_ParseSmsIntent(StartingIntent)
     End If
     Sleep(0)
     Service.StopAutomaticForeground

habilitar_push
Service_Create
        Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
        GLO_NotificationSvc.Initialize
        GLO_NotificationSvc.Icon = "icon"
        GLO_NotificationSvc.Sound = False
        GLO_NotificationSvc.SetInfo("Agendame.Turnos.Habilitar","activado!",Main)
        Service.StartForeground(123456788,GLO_NotificationSvc)
Service_Start
    Select StartingIntent.Action
        Case "com.google.android.c2dm.intent.REGISTRATION"
            HandleRegistrationResult(StartingIntent)
        Case "com.google.android.c2dm.intent.RECEIVE"
            ProcesarPushRecibido(StartingIntent)
    End Select
    Sleep(0)
    Service.StopAutomaticForeground


The Manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
)
SetApplicationAttribute(android:icon,  "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddApplicationText(<activity android:name="de.amberhome.objects.preferenceactivity"/>)   
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.SEND_SMS)
AddReceiverText(habilitar_smsread,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

AddPermission(android.permission.READ_CONTACTS)
AddPermission(android.permission.WRITE_CONTACTS)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_NETWORK_STATE)



'C2DM Permissions
AddManifestText(<permission android:name="$PACKAGE$.permission.C2D_MESSAGE" android:protectionLevel="signature" />)
AddPermission($PACKAGE$.permission.C2D_MESSAGE)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
' Push Service Receiver Attribute
SetReceiverAttribute(habilitar_push, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(habilitar_push,
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="$PACKAGE$" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="$PACKAGE$" />
</intent-filter>)

CreateResourceFromFile(Macro, Themes.DarkTheme)


AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="shared" />
)

AddPermission(android.permission.GET_ACCOUNTS)
AddPermission(android.permission.READ_PHONE_STATE)
 

Attachments

  • Screenshot_20180415-100923.png
    Screenshot_20180415-100923.png
    116.9 KB · Views: 183
  • Screenshot_20180415-100846.png
    Screenshot_20180415-100846.png
    143.6 KB · Views: 192

pesquera

Active Member
Licensed User
Longtime User
Why have you removed the "subs"
sorry, I tried to be more clear cleaning the code

Why are you making habilitar_push a foreground service?
Is that not needed?
That service is to read push messages
I don't remember now the link were I got that code.. it's possible (not sure) that I've added the lines myself.. the idea is to make the service alive all the time to respond the push messages

B4X:
habilitar_push

Sub Service_Create
        Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
        GLO_NotificationSvc.Initialize
        GLO_NotificationSvc.Icon = "icon"
        GLO_NotificationSvc.Sound = False
        GLO_NotificationSvc.SetInfo("Agendame.Turnos.Habilitar","activado!",Main)
        Service.StartForeground(123456788,GLO_NotificationSvc)
        rtn_RegisterDeviceGCM
End Sub

Sub Service_Start
    Select StartingIntent.Action
        Case "com.google.android.c2dm.intent.REGISTRATION"
            HandleRegistrationResult(StartingIntent)
        Case "com.google.android.c2dm.intent.RECEIVE"
            ProcesarPushRecibido(StartingIntent)
    End Select
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Sub rtn_RegisterDeviceGCM ' (Unregister As Boolean)
        Dim i As Intent
        i.Initialize("com.google.android.c2dm.intent.REGISTER", "")
        i.PutExtra("sender", Starter.GLO_SenderId)
       Dim jo As JavaObject = i
       jo.RunMethod("setPackage", Array("com.google.android.gms"))
       Dim r As Reflector
       Dim i2 As Intent
       i2 = r.CreateObject("android.content.Intent")
       Dim pi As Object
       pi = r.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
         Array As Object(r.GetContext, 0, i2, 0), _
         Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
       i.PutExtra("app", pi)
       StartService(i)
End Sub
 
Upvote 0
Top