What is the difference between multi-thread communication and multi-process communication in Android? How to implement them ?, Android Multithreading

Source: Internet
Author: User
Tags home screen

What is the difference between multi-thread communication and multi-process communication in Android? How to implement them ?, Android Multithreading

  When a program is started for the first time, Android starts a LINUX Process and a main thread. By default, all components of the program will run in the process and thread. In addition, Android assigns a LINUX User to each application. Android will try to keep a running process. When the memory resources are insufficient, Android will try to stop some processes and release enough resources for other new processes to use, it can also ensure that the current process being accessed by the user has enough resources to respond to the user's events in a timely manner. A thread is an integral part of a process and the basis for CPU scheduling. Generally, there are two main threads and other threads. Only the main thread can refresh the UI. After the application starts, the ActivityThread main thread is created.

Components with different package names can run in the same process in a certain way.

After an Activity is started, there are at least three threads. One main thread and two binder threads.

1. Android threads can communicate with each other in the following ways:

1) shared variables (memory)

2) Pipelines

3) handle Mechanism

RunOnUiThread (Runnable)

View. post (Runnable)

Message-driven mechanism in android processes --- Handler, MessageQueue, Runnable, Logoff

Logoff and Message Processing Mechanism: first, a handler object is created in the main thread to process the messages sent from the subthread, then, when the sub-thread needs to send a Message, the Message object will be used. The Message will first be stored in the Message queue, and the main thread also has a Looper Message polling device, messages in the Message queue will be traversed cyclically. When a Message is found, a Message will be sent to handler for processing (operations such as updating the ui). handler calls handleMessage to set the Message to null after processing, so that the Message can be recycled.

 

2. inter-process communication

Inter-process communication:

Bind Mechanism (IPC-> AIDL)

Linux shared memory

Boradcast

Data can be transmitted between activities through intent.

 

3. Several Methods for Android to terminate the process

1) Use the restartPackage (String packname) method in ActivityManager. Configure permissions in the configuration file.

2) android. OS. process. killProcess (int pid) can only terminate the process of this program.

3) System. exit ()

4) After android2.2, you cannot use the restartPackage () method. Instead, you should use the killBackgroundProcesses () method and Configure permissions.

5) Use reflection to call forceStopPackage.
  1. Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
  2. forceStopPackage.setAccessible(true);
  3. forceStopPackage.invoke(am, yourpkgname);
In the configuration file, you must add the following definition: android: sharedUserId = "android. uid. system "In addition, you need to add permissions in the configuration file: <uses-permission android: name =" android. permission. FORCE_STOP_PACKAGES "> </uses-permission>

6) use the Linux Command kill-9

7) exit to the home screen

 

Public boolean onKeyDown (int keyCode, KeyEvent event) {// event. getRepeatCount (): press the return key without repeating if (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0) {Intent home = new Intent (Intent. ACTION_MAIN); home. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); home. addCategory (Intent. CATEGORY_HOME); startActivity (home);} return super. onKeyDown (keyCode, event );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.