Application restarting method and device based on double processes and storage medium
By adopting a dual-process application restart method in mobile applications, the problems of crashes and data loss during application restart are solved, and the application is restarted stably and the user experience is improved.
Patent Information
- Application Number
- CN202411584386.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-07
- Publication Date
- 2025-02-25
AI Technical Summary
When mobile applications download and dynamically load resources, restarting the application may cause application crashes and data loss, and the existing technology is difficult to effectively solve this problem.
Using a dual-process application restart method, by waking up the second activity process in the first activity process and closing and restarting the first activity process with the second activity process, ensuring that the data is properly processed and saved during the restart process.
It realizes stable restart of the application when downloading the update resource file, avoids the loss of application data, and causes the re-download of the updated resource, improves the user's operating experience, and does not require the user to manually restart the application.
Smart Images

Figure CN119512820A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of mobile applications, and in particular to a dual-process based application restart method, device and storage medium. Background Art
[0002] In the field of mobile application development, as application functions increase and complexity increases, application stability and reliability become critical. Some applications need to download and dynamically load resource files, such as images, audio, or configuration files, to provide a richer user experience.
[0003] However, dynamically loading resource files may require the application to restart in order for the new resources to take effect. Under system restrictions, the application is prone to restart failure, requiring the user to manually close the process and restart the application. Manually closing and restarting the main process poses a greater risk to system data. If not handled properly, it may cause the application to crash or data loss, which may cause the user to dynamically load resource files again. Therefore, in view of the problem that restarting the application will cause application crashes and data loss when the current mobile application downloads and dynamically loads resources, a new technology is needed to solve the current technical problems. Summary of the invention
[0004] The main purpose of the present invention is to solve the technical problem that when a mobile application downloads and dynamically loads resources, restarting the application will cause application crash and data loss.
[0005] A first aspect of the present invention provides an application restart method based on a dual process, comprising the steps of:
[0006] Receive a restart task, and read the first process ID of the first activity process corresponding to the restart task;
[0007] In the first activity process, wake up the second activity process, and pass the first process ID as a parameter to the second activity process;
[0008] In the second activity process, closing the first activity process according to the first process ID;
[0009] In the second activity process, after confirming that the first activity process has been closed, restart the first activity process;
[0010] In the first activity process, close the second activity process.
[0011] Optionally, in a first implementation manner of the first aspect of the present invention, in the second activity process, after closing the first activity process according to the first process ID, in the first activity process, before closing the second activity process, the method further includes:
[0012] The data of the second activity process is restricted and stored in a preset cache file.
[0013] Optionally, in a second implementation of the first aspect of the present invention, in the first activity process, closing the second activity process includes:
[0014] The first activity process accesses the cache file to load resources corresponding to the second activity process;
[0015] Close the second activity process.
[0016] Optionally, in a third implementation manner of the first aspect of the present invention, passing the first process ID as a parameter to the second activity process includes:
[0017] Writing the first process ID into the Intent flag of the Intent() function corresponding to the second activity process, wherein the Intent flag is used to control the activity process state;
[0018] Set the second activity process to prohibit the intent() function from being started.
[0019] Optionally, in a fourth implementation manner of the first aspect of the present invention, closing the first activity process according to the first process ID includes:
[0020] In the second activity process, the killFormProcess() function is used to close the first activity process corresponding to the Intent flag of the Inte nt() function.
[0021] Optionally, in a fifth implementation of the first aspect of the present invention, after confirming that the first activity process has been closed, restarting the first activity process includes:
[0022] According to the preset delay time, cyclically determine whether the first activity process is closed;
[0023] When the first activity process is closed, the restriction on starting the intent() function outside the second activity process is cancelled;
[0024] Using the startActivity() function, the Intent flag of the Intent() function is restarted to correspond to the first activity process.
[0025] Optionally, in a sixth implementation manner of the first aspect of the present invention, in the first activity process, waking up the second activity process includes:
[0026] In the first activity process, the second activity process is awakened by calling the launch() function.
[0027] Optionally, in a seventh implementation manner of the first aspect of the present invention, the startup mode of the second activity process and the first activity process is set to singleTask.
[0028] A second aspect of the present invention provides an application restart device based on a dual process, comprising: a memory and at least one processor, wherein instructions are stored in the memory, and the memory and the at least one processor are interconnected through a line; the at least one processor calls the instructions in the memory so that the application restart device based on a dual process executes the above-mentioned application restart method based on a dual process.
[0029] A third aspect of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, which, when executed on a computer, enable the computer to execute the above-mentioned dual-process-based application restart method.
[0030] In the embodiment of the present invention, a new process is started to restart the main process of the app, and the main process is restarted by switching between two threads, thereby avoiding the user from manually restarting the application. When the application has more update resources, the application can be kept stable and avoid crashes and data loss when the application restarts the update resources. The application can be restarted stably when downloading the update resource file, avoiding the data loss of the application causing the update resource to be downloaded again, thus improving the user's operating experience, and eliminating the need for the user to manually restart the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 A schematic diagram of an embodiment of a dual-process based application restart method in an embodiment of the present invention;
[0032] Figure 2104 is a schematic diagram of an embodiment of the dual-process based application restart method in accordance with an embodiment of the present invention;
[0033] Figure 3 The figure is a schematic diagram of an embodiment of a dual-process application restart device in an embodiment of the present invention. DETAILED DESCRIPTION
[0034] The embodiments of the present invention provide a dual-process based application restart method, device and storage medium.
[0035] The embodiments disclosed in the present invention will be described in more detail below with reference to the accompanying drawings. Although certain embodiments disclosed in the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments disclosed in the present invention are only for exemplary purposes and are not intended to limit the scope of protection disclosed in the present invention.
[0036] In the description of the embodiments disclosed in the present invention, the term "including" and similar terms should be understood as open inclusion, that is, "including but not limited to". The term "based on" should be understood as "based at least in part on". The term "one embodiment" or "the embodiment" should be understood as "at least one embodiment". The terms "first", "second", etc. may refer to different or the same objects. Other explicit and implicit definitions may also be included below.
[0037] For ease of understanding, the specific process of the embodiment of the present invention is described below. Figure 1 , an embodiment of the dual-process based application restart method in the embodiment of the present invention includes:
[0038] 101. Receive a restart task, and read the first process ID of the first activity process corresponding to the restart task;
[0039] In this embodiment, we need at least two processes in the process, one is the main process of the application, that is, the first activity process, and the other is the process responsible for restarting, that is, the second activity process. Before restarting, ensure that resources are fully prepared, including the status information of the current operation, and try to ensure that all current tasks are terminated or closed normally. The second activity process is responsible for restarting the application, and the important real-time status of the first activity process is saved through the cache file. If the application has other auxiliary processes in addition to the main process, all auxiliary processes related to the restart can be closed first. After receiving the restart task, based on the first activity process specified by the restart task, read the first process ID of the first activity process.
[0040] 102. In the first activity process, wake up the second activity process, and pass the first process ID as a parameter to the second activity process;
[0041] In this embodiment, in the first activity process, the second activity process is launched, and the first process ID (ie, PID) is passed as a parameter to the second activity process.
[0042] Specifically, in step 102, “in the first activity process, waking up the second activity process” includes the following specific implementations:
[0043] 1021. In the first activity process, wake up the second activity process by calling the launch() function.
[0044] In this embodiment, in the first activity process, the launch() function is used to suspend the second activity process. When the second activity process is suspended, a function that passes the first process ID as a parameter to the second activity process can be nested. The following specific implementation code can be referred to:
[0045] fun launch(context:Context){
[0046] context.startActivity(
[0047] Intent(context,KillAndRestartActivity::class.java) / / Prohibit the intent() function from starting outside the second activity process
[0048] .putExtra(EXTRA_MAIN_PID,Process.myPid()) / / Write the first process ID to the second activity process / /
[0049] .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) / / Intent flags for the Intent() function.
[0050] In one embodiment, the startup mode of the second activity process and the first activity process is set to singleTask.
[0051] In this embodiment, the singleTask startup mode is one of the four startup modes of Activity in Android. When using the singleTask mode to start an Activity, the system will first check whether the Activity already has an instance in the task stack of the current application. If it does, a new instance will not be created, but the existing instance will be moved to the top of the stack. In the process of moving the existing instance to the top of the stack, the system will automatically clear all other Activities above the instance to ensure that the interface seen by the user is the latest.
[0052] Specifically, in step 102, "passing the first process ID as a parameter to the second activity process" includes the following specific implementations:
[0053] 1022. Write the first process ID into the Intent flag of the Intent() function corresponding to the second activity process, wherein the Intent flag is used to control the activity process state;
[0054] 1023. Set the second activity process to prohibit the intent() function from being started.
[0055] In steps 1022-1023, EXTRA_MAIN_PID is used as the first process ID and written into the Intent() function corresponding to the second activity process, and the first process ID is written into the Intent flag of the Intent() function through .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK). In the intent() function, it is set that the intent() function is prohibited from being started outside the second activity process.
[0056] 103. In the second activity process, closing the first activity process according to the first process ID;
[0057] In this embodiment, in the second activity process, based on the first process ID (ie, PID) as a reference, the first activity process is closed.
[0058] Further, in step 103, “closing the first activity process according to the first process ID” includes the following specific implementation methods:
[0059] 1031. In the second activity process, use the killFormProcess() function to close the Intent flag of the Intent() function corresponding to the first activity process.
[0060] In step 1031, in the second activity process, EXTRA_MAIN_PID is used as the basis for closing, and the killFormProcess() function is used to close the Intent flag of the Intent() function corresponding to the first activity process. The specific implementation process is as follows:
[0061] private fun killFromProcess(){
[0062] val pid=intent.getIntExtra(EXTRA_MAIN_PID,defaultValue:-1)
[0063] Process.killProcess(pid)
[0064] }.
[0065] Further, after step 103 and before step 105, the following specific implementation methods are included:
[0066] 1032. Store the data restrictions of the second activity process in a preset cache file.
[0067] In step 1032, the second activity process settings are stored in a cache file, and can be limited to being stored only in memory.
[0068] Based on step 1032, step 105 of “closing the second activity process in the first activity process” includes the following specific implementation methods:
[0069] 1051. The first activity process accesses the cache file to load resources corresponding to the second activity process;
[0070] 1052. Close the second activity process.
[0071] In steps 1051-1052, after restarting the first activity process, the first activity process accesses the data in the cache file, and the data in the cache file is the data of the application program running by the second activity process when the first activity process is closed, so as to avoid the first activity process losing the data running by the second activity process. After loading the data in the cache file, the second activity process is closed.
[0072] 104. In the second activity process, after confirming that the first activity process has been closed, restart the first activity process;
[0073] In this embodiment, the second activity process monitors whether the first activity process is closed, continues monitoring until it is confirmed that the first activity process is closed, and then suspends the first activity process to enable the first activity process to load the update resource.
[0074] For details, please refer to Figure 2 , Figure 2 This is an embodiment of step 104 of the dual-process application restart method in an embodiment of the present invention, and step 104 includes the following specific implementation methods:
[0075] 1041. According to the preset delay time, cyclically determine whether the first activity process is closed;
[0076] 1042. When the first activity process is closed, the restriction on starting the intent() function outside the second activity process is cancelled;
[0077] 1043. Use the startActivity() function to restart the first activity process corresponding to the Intent flag of the Intent() function.
[0078] In steps 1041-1043, the delay time is set to 200 milliseconds, the counter is set to 10, and a delay of 200 milliseconds is used to determine whether the first activity process is closed. Then, a value of 1 is subtracted from the counter, and the loop is eliminated when the counter is 0. The specific implementation method is as follows:
[0079] / *varn=10
[0080] while(n-->0){
[0081] delay(200)
[0082] if(!isMainProcessAlive()){
[0083] break
[0084] }* /
[0085] When it is determined that the first activity process is closed, the restriction on starting the intent() function outside the second activity process is cancelled. Then, the startActivity() function is used to restart the intent flag of the intent() function corresponding to the first activity process. The specific implementation code may be as follows:
[0086] if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S){
[0087] / / Remove the strict restrictions on intent launch outside the second activity process
[0088] StrictMode.setVmPolicy(
[0089] strictMode.VmPolicy.Builder(StrictMode.getVmPolicy())
[0090] .permitUnsafeIntentLaunch()
[0091] .build() )
[0093] / / Restart the second activity process
[0094] startActivity(
[0095] Intent(packageContext:this@KilLAndRestartActivity,MainActivity::class.ja va)
[0096] .addFlags(flags:Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
[0097] ).
[0098] 105. In the first activity process, close the second activity process.
[0099] In this embodiment, in the first activity process, the second activity process control is closed, and the specific practice code is as follows:
[0100] / / Close the current second activity process
[0101] finish()
[0102] Runtime.getRuntime().exit(status:0).
[0103] In actual application usage scenarios, applications may sometimes need to download a lot of resources, and then need to restart to load them into the program to run. In this process, downloading and loading resources originally take two periods of time, because a restart is required in the middle. If we use dual processes to help users restart automatically instead of asking users to restart manually, then users only need to wait quietly once, and users can do other things during this process. For example, when King of Glory is updated, it will sometimes automatically restart once after updating resources.
[0104] In the embodiment of the present invention, a new process is started to restart the main process of the app, and the main process is restarted by switching between two threads, thereby avoiding the user from manually restarting the application. When the application has more update resources, the application can be kept stable and avoid crashes and data loss when the application restarts the update resources. The application can be restarted stably when downloading the update resource file, avoiding the data loss of the application causing the update resource to be downloaded again, thus improving the user's operating experience, and eliminating the need for the user to manually restart the application.
[0105] Figure 3 1 is a schematic diagram of the structure of a dual-process based application restart device provided by an embodiment of the present invention. The dual-process based application restart device 300 may have relatively large differences due to different configurations or performances, and may include one or more processors (central processing units, CPU) 310 (for example, one or more processors) and a memory 320, and one or more storage media 330 (for example, one or more mass storage devices) storing application programs 333 or data 332. Among them, the memory 320 and the storage medium 330 may be short-term storage or permanent storage. The program stored in the storage medium 330 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the dual-process based application restart device 300. Furthermore, the processor 310 may be configured to communicate with the storage medium 330 to execute a series of instruction operations in the storage medium 330 on the dual-process based application restart device 300.
[0106] The dual-process based application restart device 300 may further include one or more power supplies 340, one or more wired or wireless network interfaces 350, one or more input and output interfaces 360, and / or one or more operating systems 331, such as Windows Serve, Mac OS X, Unix, Linux, FreeBSD, etc. Those skilled in the art will appreciate that Figure 3 The illustrated dual-process based application restart device structure does not constitute a limitation on the dual-process based application restart device, and may include more or fewer components than illustrated, or a combination of certain components, or a different arrangement of components.
[0107] The present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. Instructions are stored in the computer-readable storage medium. When the instructions are executed on a computer, the computer executes the steps of the dual-process-based application restart method.
[0108] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0109] In addition, although each operation is described in a specific order, this should be understood as requiring such operation to be performed in the specific order shown or in a sequential order, or requiring that all illustrated operations should be performed to obtain desired results. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single implementation in combination. On the contrary, the various features described in the context of a single implementation can also be implemented in multiple implementations individually or in any suitable sub-combination mode.
[0110] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
Claims
1. A dual-process based application restart method, characterized in that: Includes steps: Receive a restart task, and read the first process ID of the first activity process corresponding to the restart task; In the first activity process, wake up the second activity process, and pass the first process ID as a parameter to the second activity process; In the second activity process, closing the first activity process according to the first process ID; In the second activity process, after confirming that the first activity process has been closed, restart the first activity process; In the first activity process, close the second activity process.
2. The dual-process based application restart method according to claim 1, characterized in that: In the second activity process, after closing the first activity process according to the first process ID, and before closing the second activity process in the first activity process, the method further includes: The data of the second activity process is restricted and stored in a preset cache file.
3. The dual-process based application restart method according to claim 2, characterized in that: In the first activity process, closing the second activity process includes: The first activity process accesses the cache file to load resources corresponding to the second activity process; Close the second activity process.
4. The dual-process based application restart method according to claim 1, characterized in that: The passing the first process ID as a parameter to the second activity process includes: Writing the first process ID into the Intent flag of the Intent() function corresponding to the second activity process, wherein the Intent flag is used to control the activity process state; Set the second activity process to prohibit the intent() function from being started.
5. The dual-process based application restart method according to claim 4, characterized in that: The closing of the first activity process according to the first process ID includes: In the second activity process, the killFormProcess() function is used to close the Intent flag of the Intent() function corresponding to the first activity process.
6. The dual-process based application restart method according to claim 5, characterized in that: After confirming that the first activity process has been closed, restarting the first activity process includes: According to the preset delay time, cyclically determine whether the first activity process is closed; When the first activity process is closed, the restriction on starting the intent() function outside the second activity process is cancelled; Using the startActivity() function, the Intent flag of the Intent() function is restarted to correspond to the first activity process.
7. The dual-process based application restart method according to claim 1, characterized in that: In the first activity process, waking up the second activity process includes: In the first activity process, the second activity process is awakened by calling the launch() function.
8. The dual-process based application restart method according to claim 1, characterized in that: The startup mode of the second activity process and the first activity process is set to singleTask.
9. A dual-process based application restart device, characterized in that: The dual-process based application restart device comprises: a memory and at least one processor, the memory stores instructions, and the memory and the at least one processor are interconnected via a line; The at least one processor calls the instruction in the memory to enable the dual-process based application restart device to execute the dual-process based application restart method according to any one of claims 1 to 8.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the dual-process based application restart method according to any one of claims 1 to 8 is implemented.