Convert between message and string in android?

I am trying to control the network connection on Android. This cannot be done in the main thread, so the data is collected in a separate stream that I create - and I learn how to use the Handler class to send reports to the user interface every second. Here are the relevant snippets of my code ...

public class MainActivity extends Activity { private TextView textView; private int linkSpeed; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Message message = msg; textView.setText(message); setContentView(textView); } }; /** Called when the activity is created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); textView = new TextView(this); textView.setTextSize(25); Thread thread = new Thread() { public void run() { for (;;) { try { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); while (true) { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS } else { linkSpeed = -1; } } String message = "linkSpeed = " + linkSpeed; handler.handleMessage(message); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } }; thread.start(); } 

The problem is that the message I receive from the new thread that I create is of type β€œString”, but I cannot overload the handleMessage method in the Handler class to use strings instead of strings. I don’t think there is a direct conversion between strings and messages, since the only method of the Message class that returns String is the toString () method, but it returns a description, not what the message contains. I'm also confused about how I can convert String to Message - and I feel like I'm doing a very roundabout approach. Any help would be greatly appreciated!

+4
source share
2 answers

You do not need to overload or try to do any kind of cover between String and Message . What you have to do is put this String in an object of type Message and send it to Handler . Then, in handleMessage() extract the string from the message.

Something like that:

 // .... String message = "linkSpeed = " + linkSpeed; Message msg = Message.obtain(); // Creates an new Message instance msg.obj = message; // Put the string into Message, into "obj" field. msg.setTarget(handler); // Set the Handler msg.sendToTarget(); //Send the message //.... 

And in handleMessage() :

 @Override public void handleMessage(Message msg) { String message = (String) msg.obj; //Extract the string from the Message textView.setText(message); //.... } 

But besides this, the program has a problem: you cannot send data to the handler, because this part of the code is not available:

 while (true) { WifiInfo s = wifiManager.getConnectionInfo(); //.. } String message = "linkSpeed = " + linkSpeed; // This line never won't be reached. 

Also, be sure to stop Thread at some point, otherwise it will continue to work even after you close the application.

+16
source

You can attach objects β€” for example, your String β€” to your Message objects using the Bundle.

I created this example:

 import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class MainActivity extends Activity { Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Bundle bundle = msg.getData(); String text = bundle.getString("key"); // text will contain the string "your string message" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread thread = new Thread() { @Override public void run() { Message message = handler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("key", "your string message"); message.setData(bundle); handler.sendMessage(message); } }; thread.start(); } } 
+6
source

Source: https://habr.com/ru/post/1488761/


All Articles