Replacing deprecated while using Google Drive API

In my android app I have an option to backup the database to Google Drive. For that I am using DriveServiceHelper class, but I just noticed, in Android 11 the is deprecated.

This is a part of the DriveServiceHelper class:


      public Task<FileList> queryFiles() {
    return, () ->

And from my BackupActivity then I call queryFiles from backup method:


public void backup(View v) {
                .addOnSuccessListener(fileList -> {
                  // another code
                .addOnFailureListener(e -> showMsgSnack(getString(R.string.uploaderror)));

I did not find any solution how to deal with this to avoid complete rework of that class.

What I tried:

I tried to replace with runnable, also callable, but it doesn’t work as Task is expected to be returned, not Filelist.

also I tried to use TaskCompletionSource:


public Task<FileList> queryFiles(int delay) throws IOException {
        TaskCompletionSource<FileList> taskCompletionSource = new TaskCompletionSource<>();
        FileList result = mDriveService.files().list().setSpaces("drive").execute();
        new Handler().postDelayed(() -> taskCompletionSource.setResult(result), delay);
        return taskCompletionSource.getTask();


But when I call queryFiles(1000) from my main class, it gives me error:

java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock.

I’ll appreciate any help.

Sharing is caring!

Leave a Reply