In Android, an open-source mobile phone operating system, there are a lot of application skills and modification methods worth mastering to change the performance of mobile phones and meet our application needs. Here we will introduce in detail how to pass the Android value.
- Android Widget Toolkit
- Basic concepts of the Android Menu System
- Summary of Android Intent application practices
- How Android databases work
- Android component concepts
When an application contains multiple activities, it is a problem to pass the values before each Activity. So here we will introduce the relevant methods.
In Android, two methods are used: PendingIntent and broadcast.
According to the category, I divided the classes related to Android value passing.
1. Intent, Context, BroadcastReceiver
2. method class PendingIntent Context. sendBroadcast
The passed method is the two methods in the method class.
The literal meaning of A Intent is Intent. In fact, its effect is similar to that of the word. Many books of Gao hentang have detailed introductions.
He is like a courier, and he is an omnipotent courier. Everything can be delivered, as long as you can add the information to him.
Detailed usage can be found in the SDK. If you want to add the content you want, you can use the Intent. set ** series method.
The translation of B Context into a better environment
As its name implies, he just gets our current environment status. For example, I want to know what language, status, flight, and mute the current user. Have the user answered the call?
And so on, just like we get the system time. Context also sets a container for us to obtain the status of the system.
Because the system status is very important, sometimes it is based on the system status, so many Intent methods need to be added to this Intent. context
C BroadcastReceiver is a class specifically used to receive broadcasts.
So he has an OnReceive () method and the specific acceptance is described below.
α PendingIntent this method is to directly pass Intent
Now, how can we ensure that the Android value can be received? Because Intent is a very powerful courier, he can record the location where the goods need to arrive, so what is PendingIntent? It is equivalent to an Intent workplace, he is only responsible
Intent outgoing
The Intent can go there and tell the Intent directly.
The following statement can be used to display intent. setClass (Activity. this // refers to Activity, Report. class // refers to the target class );
How does the receiver obtain messages? This is also simple. Because Intent has come to its location according to its own address, we can simply instantiate it.
Of course, this is in a simple situation, that is to say, if this company only has one cargo, we need one courier. At present, the receiver only needs to receive this cargo, when the courier arrives at his receiver, he will know what he is doing after confirmation.
But in most cases, the business of the two companies is very busy, so there may be multiple couriers, multiple goods, and one department may have multiple. So how can we do it. You need to add a limited rule
(1) first, add a limit to the Intent. You can set a Permission String, just like a dark sign. If the dark sign is correct, it cannot be wrong. This is very important in the broadcast method)
(2) The other is to determine the parameter of the instance during instantiation.
Beta Android uses broadcast for data transmission
If you directly use PendingIntent for purposeful transmission, the broadcast is actually a mass transfer of information.
In fact, the content transmitted in Broadcast is also Intent, but it can be transferred in a far range.
In addition, many other programs in the system are also sending broadcasts, such as the status of the mobile phone and the status of the flight. If the status is determined by the flight status, the system will automatically send broadcasts, we only need to receive the message so that we can know that the system is broadcasting now.
In terms of the display method of widgets, The AppWidget inherits BroadcastReciever, so it is pure and will not cause any problems. In addition, this is my personal judgment, because the Widget is still different from the ordinary app, I guess if you send Intent directly to the Widget, it will be restricted, just like handler, of course this is my guess, but it still feels reliable with broadcast.
The carrier of broadcast is Intent, but their label is Action. Many actions are set in the system.
Of course, you can also define your own Intent. Intent ("your own defined string ")
The recipient needs to register
RegisterReceiver (intent, commandFilter );
The second parameter is Intent filtering and cannot be blank.
If it is a custom Action, you still need
- String action = intent. getAction ();
- If (action. equals ("Custom Action "))
- {Intent instantiation must be performed here.
- }
Σ widgets use PendingIntent because there are no other methods. To be honest, AppWidget will give us a method that can use RemoteViews, and nothing else can be used. So I wrote a SetPeningIntent. Of course, this method also has a lot of fixed usage, starting the service, starting the activity, and sending the broadcast, basically all of which are available.
3. Finally, let's talk about the Action as a string, but this string is very long, so few people basically overlap with it.
They define some States or aviation states, and their actions are
ACTION_AIRPLANE_MODE_CHANGED
The preceding section describes how to pass Android values.