Android Question PhoneWakeState And Timer In DeepSleep

Hakanunver

Member
Licensed User
Hi B4A, I try to make an app which uses Xmpp with asmack library.
I can connet to server, get or send message. App is stable with xmpp.

When phone/cpu goes deep sleep. I can get messages and run media player to make alarm but i can't open my application with;

XmppService:
B4X:
Sub Process_Globals
    Dim p As PhoneWakeState
End Sub

Sub OpenApp
    Try
        If main_status = False Then
            StartActivity(Panel)
            p.KeepAlive(True)
        End If
    Catch
        Log(LastException)
    End Try
End Sub

Sub pingBaglantiHatasi(varmi As Boolean)
    If varmi = True Then
    OpenApp
    sescal
    End If
End Sub

I run this code in my XmppService Module and cannot open app

My another problem is I wants to ping my server to learn is it achievable when device in deep sleep. So i tried

XmppServerPing Servce Module :
B4X:
Sub Service_Start (StartingIntent As Intent)
    XmppService.objXmpp.BaglantiVarMi
    'StartServiceAt("", DateTime.Now+15*1000, True)
    'StartServiceAtExact("",DateTime.Now+15*1000,True)
    'SetExactAndAllowWhileIdle(DateTime.Now+15*1000,"XmppServerPing")
    SetAlarm(DateTime.Now+30*1000)
End Sub

'Sub SetExactAndAllowWhileIdle (Time As Long, ServiceName As String)
'    Dim p As Phone
'    If p.SdkVersion < 23 Then
'        StartServiceAtExact(ServiceName, Time, True)
'    Else
'        Dim in As Intent
'        in.Initialize("", "")
'        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
'        Dim jin As JavaObject = in
'        jin.RunMethod("setAction", Array(Null))
'        Dim ctxt As JavaObject
'        ctxt.InitializeContext
'        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
'        Dim pi As JavaObject
'        pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getService", _
'  Array(ctxt, 1, in, 134217728))
'        am.RunMethod("setExactAndAllowWhileIdle", Array(0, Time, pi))
'    End If
'End Sub

Sub SetAlarm(timeToAlarm As Long)
    'declare variable for java object
    Dim srvAt As JavaObject
    'call the function
    srvAt.InitializeContext
    srvAt.RunMethod("StartServiceAtExactWhileIdle", Array(timeToAlarm, True))
End Sub


#If JAVA
//import android.app.Service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

public void StartServiceAtExactWhileIdle(Long time, Boolean wakeUp){
  AlarmManager alMan = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(this.getApplicationContext(), this.getClass());
  PendingIntent penInt = PendingIntent.getService(this.getApplicationContext(), 1, intent, 134217728);
  alMan.setExactAndAllowWhileIdle(wakeUp?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, time, penInt);
}
#End If

XmppService.objXmpp.BaglantiVarMi is my xmpp libraries function it pings to server and return true or false to my
pingBaglantiHatasi function in XmppService

Where i m wrong or what may i do ? Thanks.
 

Hakanunver

Member
Licensed User
I start my XmppService with StartService at the opening of the app and i use;

Service.StartForeground(1, Notification)

at Service_Start

XmppService:
B4X:
Sub Service_Create
        Logger.Initialize(File.DirDefaultExternal , "TheLogFile" )
        If Notification.IsInitialized = False Then
            Notification.Initialize
            Notification.Icon = "icon"
            Notification.SetInfo("My Service", "My Service", Main)
            Notification.Sound = False
            Notification.Vibrate = False
        End If
        If ph.SdkVersion >= 9 Then
            Dim r As Reflector
            r.Target = r.CreateObject("android.os.StrictMode$ThreadPolicy$Builder")
            r.Target = r.RunMethod("permitAll")
            r.Target = r.RunMethod("build")
            r.RunStaticMethod("android.os.StrictMode", "setThreadPolicy", _
            Array As Object(r.Target), Array As String("android.os.StrictMode$ThreadPolicy"))
        End If
       
        BroadCast.Initialize("BroadcastReceiver")
        BroadCast.addAction("android.net.conn.CONNECTIVITY_CHANGE")
        BroadCast.addAction("")
        BroadCast.SetPriority(2147483647)
        BroadCast.registerReceiver("")

End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1, Notification)
End Sub

My Service_Create and Service_Start like that
I think XmppService never close.
 
Upvote 0

Hakanunver

Member
Licensed User
I checked before battery optimizations and

Smart battery save is close
Optimization is close
I don t have cleaner app or mobile device management app

My Service doesnt kill some other apps because it takes xmpp messages only thing is about p.keepalive(True)

is it can be about p.RealeseKeepAlive ?
 
Upvote 0
Top