Android data transmission Overview

Source: Internet
Author: User

In the Android operating system, data transmission and implementation methods are diversified. You can choose a method that suits your needs for operations. Here we will first introduce the Android data transmission methods in detail.

First, we use the most data transfer between activities in Android.

Value Transfer between activities

We use Intent to pass values between activities. This part is believed to be a little basic, and everyone will know that we use the Extra part of Intent to store the data we want to transmit. Example:

 
 
  1. Intent i = new Intent(this, YourClass.class);   
  2. i.putExtra(YOURDATA, data);   
  3. startActivity(i);   
  4. Intent i = new Intent(this, YourClass.class);  
  5. i.putExtra(YOURDATA, data);  
  6. startActivity(i);  

Note that the putExtra method must have a package prefix for the first parameter. That is to say, we cannot specify a String at will, but must carry the package prefix, for example, we can define YOURDATA as follows:

 
 
  1. public final static String YOURDATA = 
    "com.javaeye.notfatboy.testArg";   
  2. ublic final static String YOURDATA = 
    "com.javaeye.notfatboy.testArg";  

The second parameter can be int, long, char, and many other types. For details, see putExtra ()

  • How Android judges the network status
  • Android unit test source code
  • Android Jni sample code
  • How to install and uninstall a program on Android
  • Android Shell commands

In fact, Intent is widely used. We can transmit values through Extra in any place where Intent is used. In other words, we can also transmit values to the Service, this also illustrates another problem: we can pass values between different processes and threads in this way, because Activity, Service, broadcast Components can be in different processes or threads. This is also a lightweight process provided by Android for us to communicate with each other through threads.

In addition, Android provides a slightly more complex value transfer mechanism. For Thread, we can use Message Queue to transmit Android data.

Messag Queue

Message Queue is a very useful and interesting mechanism. Suppose we have two threads, one is Thread A and the other is thread B. Thread A has A Message Queue and corresponds to A Handler to process the Message. This means that thread B can send A Message to Thread A as long as it obtains the reference of the Handler. Example:

 
 
  1. public class ThreadB extends Thread {   
  2. ....   
  3. public void run(){   
  4. ...   
  5. String str = "Test String";   
  6. Message msg = mHandler.obtainMessage(1, str);   
  7. mHandler.sendMessage(msg);   
  8. ..   
  9. }   
  10. }   
  11. public class ThreadB extends Thread {  
  12. ....  
  13. public void run(){  
  14. ...  
  15. String str = "Test String";  
  16. Message msg = mHandler.obtainMessage(1, str);  
  17. mHandler.sendMessage(msg);  
  18. ..  
  19. }  

Of course, there are many other ways to use Message Queue. Here we will not repeat them here. If you are interested, you can study it carefully.

For IPC process communication, Android also provides a slightly more complex mechanism-AIDL

AIDL

In Android, we can use AIDL to define specific interfaces to implement RPC, instead of simply passing values. Many articles have introduced detailed usage of this part, I will not go into details.

Android data transmission methods are described here.

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.