从后台启动 activity,不会 运行 onResume() 中的函数
Launching activity from the background, doesn't run the function in onResume()
在我的应用程序中,当应用程序停止录制音频时,它会在 onResume() 中启动另一个 activity 和 运行s 函数 automaticSync(),但是当设备被锁定时它不会运行 后台运行的函数,有没有办法在第二个 activity 启动时从后台运行 运行 一个函数。
正在录制 Activity 启动另一个 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
}
录音浏览器
@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();
}
}
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.
OnResume() 仅在 phone 未处于锁定状态时有效。
您可以尝试其他方法,如后台服务、广播接收器,或者您可以使用 onStop 方法覆盖
在我的应用程序中,当应用程序停止录制音频时,它会在 onResume() 中启动另一个 activity 和 运行s 函数 automaticSync(),但是当设备被锁定时它不会运行 后台运行的函数,有没有办法在第二个 activity 启动时从后台运行 运行 一个函数。
正在录制 Activity 启动另一个 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
}
录音浏览器
@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();
}
}
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.
OnResume() 仅在 phone 未处于锁定状态时有效。 您可以尝试其他方法,如后台服务、广播接收器,或者您可以使用 onStop 方法覆盖