Android Question Sleep behavior different in Android P?

Computersmith64

Well-Known Member
Licensed User
Hi All,

Due to my Nexus 5X completely failing from a very common hardware issue, I now have a Pixel running Android 9 - which is my first device to do so.

Back when Erel announced that DoEvents was being deprecated & that Sleep should be used instead, I somewhat indiscriminately went through most of my apps & did exactly that. In hindsight, I probably should have been a bit more careful about it, but I've found that all those apps generally work fine - up until now.

During an update to myPets, testing showed that Android 9 seems to handle Sleep(0) differently to previous Android versions in some cases. There are many places in the code where I am discovering this, but one good example is the code below.

As I do a lot in this app, I show a progress dialog for functions that might take a little while to complete. You can see in the code below a commented out Sleep(0) below the call to ProgressDialogShow("Deleting..."). In the old days, I had a DoEvents there to give the UI a chance to display the dialog & in my pre-9 code I had the Sleep(0) uncommented & everything worked fine. However with Android 9 & the Sleep(0) uncommented, the records never get deleted - presumably because the Sleep causes the sub to return & never come back. I don't understand why.

B4X:
Public Sub removePet(ID As Int)
    Private mRec As Map
   
    ProgressDialogShow("Deleting...")
    'Sleep(0)
    mRec.Initialize
    mRec.Put("ID", ID)
    Starter.sqlDB.BeginTransaction
    Try
        DBUtils.DeleteRecord(Starter.sqlDB, "tblPets", mRec)
        mRec.Clear
        mRec.Put("PetID", ID)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblHistoryHeader", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblAppointments", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblMedical", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblAlerts", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblWeight", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblAlbum", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblTraining", mRec)
        DBUtils.DeleteRecord(Starter.sqlDB, "tblHiddenPets", mRec)
        Starter.sqlDB.TransactionSuccessful
        If File.Exists(File.DirInternal, ID & ".jpg") Then File.Delete(File.DirInternal, ID & ".jpg")
        ProgressDialogHide
    Catch
        Starter.CL.CLog(LastException.Message)
        Log(LastException)
        ProgressDialogHide
    End Try
    Starter.sqlDB.EndTransaction
End Sub

I have a whole lot of other similar issues where using Sleep(0) causes unexpected results with Android 9, but no issues with earlier versions. Has anybody else seen this?

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Not me. I'm using Android 9 for several months now. Sleep is not an OS feature. My guess is that the problem is somewhere else.

Note that a better approach will be to use the async SQL methods. They are simple and will not affect the responsiveness.

Thanks - I've been going through the code & it only seems to be an issue if I use Sleep(0) after calling ProgressDialogShow. In reality, most of these functions / queries execute pretty fast anyway & it's only on my old Galaxy S3 that I ever really see the progress dialogs pop up. I have removed the Sleep(0) from after all the calls & everything seems to be working fine, plus when needed I'm still seeing the progress dialog pop up.


- Colin.
 
Upvote 0
Top