Android Question start activity from service not work in all device

Discussion in 'Android Questions' started by syerif, May 23, 2019.

  1. syerif

    syerif Member Licensed User

    Hi i create some project with start activity from service, in my device it work as expected (activity transparent is show panel)but not work for another device (Activity always on background).

    Code:
    Sub Service_Start (StartingIntent As Intent)
        
    'nb6.h
        'If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
        'If StartingIntent.IsInitialized Then
        StartServiceAt(""DateTime.Now + 5 * DateTime.TicksPerSecond, True'schedule the next task to run in 30 seconds.
        Sleep(0)
        CheckNotif
        
    'End If
        Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    End Sub
    Sub CheckNotif
    'POST API
    'load JSON return format to string
    ReadJSON
    End Sub
    Sub ReadJSON
            
    If Sensor1=0 Or Sensor2=0 Or Sensor3=0 Or Sensor3=0 Or Sensor4=0 Or Sensor5=0 Or Sensor6=0 Or Sensor7=0 Or Sensor8=0 Or Sensor9=0 Or Sensor10=0 Or Sensor11=0 Or Sensor11=0 Then
               
                
    StartActivity(Notif)
            
    Else if Sensor1>0 Or Sensor2>0 Or Sensor3>0 Or Sensor3=0 Or Sensor4=0 Or Sensor5=0 Or Sensor6=0 Or Sensor7=0 Or Sensor8=0 Or Sensor9=0 Or Sensor10=0 Or Sensor11=0 Or Sensor11=0 Then
                
    CallSub(Notif,"Button1_Click")
            
    End If

        
    Next
    End Sub
    Please any advice...
     
    Last edited: May 23, 2019
  2. DonManfred

    DonManfred Expert Licensed User

    Android does not allow you to restart the service in this REALLY REALLY REALLY short intervals.

    You should change it to at least 15 or 30 MINUTES.

    Consider using a foreground service
    Example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/
     
    Peter Simpson likes this.
  3. syerif

    syerif Member Licensed User

    Ok, it will show my activity correctly to? Because my problem is activity work in background not start correctly in some real device, but it work at my device (in 5,15 or 30 minutes)
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

  5. syerif

    syerif Member Licensed User

    For callsub routine it is work perfect in mycode. callsub in my code for close activity where was start, it triger by off fire sensor. On fire sensor triger show activity. And my problem my activity run in background in some real device but in my real device as debuger and install app it work perfect.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Tip: remove all the sensor variables and use an array instead.

    Do you have access to the problematic device? Does it always fail to start the activity? Are you sure that StartActivity is called (add Log("starting")) ?
     
    Peter Simpson likes this.
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    is the type of Sensor1 is float? then its not good to compare with =0
    in the worst case write a log into file (enabled by user) and let the user share this data via mail.
     
  8. syerif

    syerif Member Licensed User

    Th
    Thanks erel,
    it work when main activity is loaded, but it does not when it minimize or clear from list.
     
  9. syerif

    syerif Member Licensed User

    Sensor1 data type is integer like database field data type
     
    MarkusR likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

  11. MarkusR

    MarkusR Well-Known Member Licensed User

    i am more confused about this part, why should the activity come in front here?
    Code:
    Else if Sensor1>0 Or Sensor2>0 Or Sensor3>0 Or Sensor3=0 Or Sensor4=0 Or Sensor5=0 Or Sensor6=0 Or Sensor7=0 Or Sensor8=0 Or Sensor9=0 Or Sensor10=0 Or Sensor11=0 Or Sensor11=0 Then
                
    CallSub(Notif,"Button1_Click")
            
    End If
     
  12. syerif

    syerif Member Licensed User

    This part for close activity where opened, code not correctly at all just for test i am use 2 sensor.
     
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