Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the Android rewriter by receiving UDP messages

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how Android rewrites the program configuration by receiving UDP messages. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

After the Android program is packaged and released as APK, how to implement it if there are parameters that need to be modified according to different application environments?

For example, there is an application scenario in which the Android program needs to call the server-side WebService service for backend database operations, and the IP address of WebService varies with each production environment. It is obviously inconvenient to recompile the package every time because of the address change. How flexible it would be if we could find a way to dynamically modify this address parameter configuration without making any changes to the APK program.

There are other scenarios that require APK applications to change and remember our settings at any time, such as an Android TV screen that needs to display a HTML page report in real time, then our Web page address needs to be sent to this APK application to remember our settings, and the next time the program opens, it can automatically display to our report page.

To achieve these requirements, a relatively simple way is to send UDP messages to APP applications and send some configuration parameters needed by APP to it. After APP receives the message and takes the parameters, it saves them in a specific location of the mobile phone system, and the program can read these settings after opening them later.

In Android systems, every time an APP application is installed, a new folder named by the application package name is created in the system's data/data directory. This folder contains the file resources of the application, which is called internal storage files. Although the folder cannot be seen on phones without ROOT, it actually exists. We can create our own storage files in this folder, such as creating a configuration text to store some of the application's parameter configuration data. After the application is uninstalled, the contents of these files disappear naturally. Therefore, we can create our own configuration file under this path and save the parameters received through UDP in that file.

Receiving the UDP message is blocking, which means we don't move on to the next step until we receive the UDP message, so we put the process in a separate thread. The following creates a thread class dedicated to receiving fixed-port UDP messages and writes the received message text to the text file specified under data/data.

Public class UdpReceiveThread extends Thread {private final String TAG = "UdpReceiveThread"; private Context context; / / this constructor is used to pass in the context of the main active thread public UdpReceiveThread (Context c) {context = c;} @ Override public void run () {while (isAlive ()) try {sleep (1000); DatagramSocket socket = new DatagramSocket (8808) / / fixed as 8808 byte data [] = new byte [1024]; DatagramPacket packet = new DatagramPacket (data, data.length); socket.receive (packet); / / blocking, packet String result = new String (packet.getData (), packet.getOffset (), packet.getLength ()) of the receiver and sender. / / write messages received from the UDP port to the Android internal storage file as program configuration parameters try {FileOutputStream fos = context.openFileOutput ("MyScan.txt", Context.MODE_PRIVATE); OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8"); osw.write (result); osw.flush () Fos.flush (); osw.close (); fos.close ();} catch (Exception e) {Log.d (TAG, "run:" + e.getMessage ()); break }} catch (Exception e) {Log.d (TAG, "run:" + e.getMessage ()); break;}}

Add the following code to the onCreate function of the main Activity activity, read the program configuration from the data/data path of the mobile phone system, call the child thread that receives the UDP message, and pass the active context to the child thread.

Public void onCreate (Bundle savedInstanceState) {. / / read configuration try {FileInputStream fis = openFileInput ("MyScan.txt") from the Android internal storage file; InputStreamReader isr = new InputStreamReader (fis, "UTF-8"); char [] input = new char [fis.available ()]; / / available () the length isr.read (input) used to get the contents of the file; isr.close () Fis.close (); String str = new String (input); ws_url = str;} catch (Exception e) {Log.d (TAG, "onCreate:" + e.getMessage ());}. / / call UDP receiving thread loop listening port message UdpReceiveThread udpThread = new UdpReceiveThread (getApplicationContext ()); udpThread.start ();...}

Therefore, when the program is running, once it receives the UDP message from the fixed port, it will rewrite the configuration and run with the new configuration parameters the next time it starts.

After reading the above, do you have any further understanding of how Android is configured by receiving UDP message rewriting programs? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report