从后台启动活动,不在onResume()中运行该功能
在我的应用程序中,当应用程序停止录制音频时,它会启动另一项活动并在onResume()中运行函数自动系统(),但是当设备锁定时,它不会从后台运行该函数,是否可以运行一个方法启动时从背景中的第二个活动中的功能。
录制活动启动另一个活动
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
onResume 是该应用程序与用户交互的状态。当应用在前景时,这就是称为。正如您所说的“当设备被锁定时,它不会从后台运行该功能” ,是的,这是正确的。 onResume 只有当应用程序在前景中才会在procgroum 。当您的活动不再可见对用户或背景中时,它已输入 onstop 状态。看看 activity-activity-contivation-lifecycle-concept 。
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()仅在手机不处于锁定状态时起作用。
您可以尝试其他方法,例如背景服务,广播接收器,也可以使用Onstop方法覆盖
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