skip to Main Content

In my application, when the app stops recording audio it launches another activity and runs the function automaticSync() in the onResume(), but when the device is locked it does not run the function from the background, is there a way to run a function in the second activity from the background when launched.

Recording Activity launching another activity

if (autoSyncSwitchSelected1) { // If user's preference is automatic syncing then set the 
                               // activity to syncRecording which will call the 
                               // automaticSync() method
                                    
   handleSelectedFiles("syncRecordings", 3); // Start activity RecordingsExplorer for result
                            
}

Recording Explorer

@Override
protected void onResume() {
    super.onResume();
    System.out.println("onResume");
    // get default shared preferences file:
    settings = PreferenceManager.getDefaultSharedPreferences(this); 
    autoSyncSwitchSelected = settings.getBoolean(Config.PREF_AUTO_SYNC, true);
    if(autoSyncSwitchSelected) {
        automaticSync();
    }
}

2

Answers


  1. OnResume is the state in which the app interacts with the user. It’s called when the app is in the foreground. As you said "when the device is locked it does not run the function from the background", yes this is correct. OnResume will only be called when the app is in the foreground. When your activity is no longer visible to the user or in the background, it has entered the OnStop State. Have a look on Activity-lifeCycle-Concept.

    Login or Signup to reply.
  2. the OnResume() only works when the phone is not in locked state.
    You can try other methods like Background Service, Broadcast Receiver or you can override using onStop method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search