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:
- Intent i = new Intent(this, YourClass.class);
- i.putExtra(YOURDATA, data);
- startActivity(i);
- Intent i = new Intent(this, YourClass.class);
- i.putExtra(YOURDATA, data);
- 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:
- public final static String YOURDATA =
"com.javaeye.notfatboy.testArg";
- 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:
- public class ThreadB extends Thread {
- ....
- public void run(){
- ...
- String str = "Test String";
- Message msg = mHandler.obtainMessage(1, str);
- mHandler.sendMessage(msg);
- ..
- }
- }
- public class ThreadB extends Thread {
- ....
- public void run(){
- ...
- String str = "Test String";
- Message msg = mHandler.obtainMessage(1, str);
- mHandler.sendMessage(msg);
- ..
- }
- }
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.