Handler instance
The subclass needs to inherit the Hendler class and rewrite the handleMessage(Message MSG) method that accepts thread data, depending on the specific instance. The important parts are commented:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private WebView wv =null;
private Handler h = new Handler();
public final String TAG = “UNISDP”;
public final String MY_LOG = “my_log”;
MyHandler myHandler;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView) findViewById(R.id.web);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setSaveFormData(false); webSettings.setSavePassword(false); webSettings.setSupportZoom(false); wv.addJavascriptInterface(new runJavaScript(), "myjs"); wv.loadUrl(sdFilePath); myHandler = new MyHandler(); // When a new Handler instance is created, it is bound to the message queue of the current thread and starts distributing data MyThread m = new MyThread(); Thread getDataFromServer_thread = new Thread(m); getDataFromServer_thread.start();Copy the code
} Fetch thread retrieves data from the pager server
class MyThread implements Runnable { public void run() { Message msg ; Bundle b ; while(true) { try { Thread.sleep(1000); msg = new Message(); b = new Bundle(); B.pustring (“value”, “data sent to the UI thread…”) ); msg.setData(b); MainActivity.thiswww.sangpi.commyHandler.sendMessage(msg); // Send a message to the Handler. If the electron is more intuitive, you can also refer to the following format:
Swam: page www.sangpi.com
} catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code
Class MyHandler extends Handler {public MyHandler() {} public MyHandler(Looper L) {} Public MyHandler(Looper L) { { super(L); Override public void handleMessage(MSG) {super.handleMessage(MSG); // Update UI Bundle b = msg.getData(); String strValue = b.getString(“value”); //MainActivity.this.button.append(color); Log.i(MY_LOG,strValue); }}}