Random, sequential, and inverted loading of a WebView group of Android devices

Source: Internet
Author: User

Random, sequential, and inverted loading of a WebView group of Android devices

An application is written to implement the order, reverse order, and random loading of a set of webviews. When latency is used, why? See:



Package com. zms. csdngo; import android. content. context; import android. content. intent; import android.net. connectivityManager; import android.net. networkInfo; import android.net. wifi. wifiManager; import android. OS. handler; import android. OS. message; import android. OS. powerManager; import android. support. v7.app. actionBarActivity; import android. OS. bundle; import android. util. log; import android. view. menu; import Android. view. menuItem; import android. view. view; import android. view. window; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; import java.net. URL; import java.net. URLConnection; import java. util. date; import java. util. random; public class Main extends ActionBarActivity {Private String urlIndex = "http://blog.csdn.net/zhoumushui/article/details/"; private WebView mWebView; private String webPath; private int count = 0; // The currently loaded page ID private int reverseCount = 0; private int realCount = 0; // Number of loaded pages private int limitCount = 0; // The maximum number of loaded pages. When this value is reached, the application exits; if the value is 0, private Boolean isLimit = false is not restricted; private TextView tvState = null; private TextView mTimeHint = null; private T ExtView mTimeCount = null; private TextView tvWebHour = null; private TextView tvLimit = null; private EditText etLimit = null; private int loadMode = 3; // 1-sequential loading; 2-reverse loading; 3-Random Loading of private Button btnShowHide; private Button btnChangeMode; private static PowerManager. wakeLock wakeLock; private int timeSec = 0; // number of seconds. use private int webHour = 8 for timing; @ Override protected void onCreate (Bundle savedInstanceSta Te) {requestWindowFeature (Window. FEATURE_NO_TITLE); super. onCreate (savedInstanceState); setContentView (R. layout. main); mWebView = (WebView) findViewById (R. id. mWebView); tvState = (TextView) findViewById (R. id. tvState); mTimeHint = (TextView) findViewById (R. id. mTimeHint); mTimeCount = (TextView) findViewById (R. id. mTimeCount); tvWebHour = (TextView) findViewById (R. id. tvWebHour); tvLimit = (TextView) FindViewById (R. id. tvLimit); etLimit = (EditText) findViewById (R. id. etLimit); btnShowHide = (Button) findViewById (R. id. btnShowHide); btnChangeMode = (Button) findViewById (R. id. btnChangeMode); initModeBtn (); initUrl (); count = 0; reverseCount = urlArray. length-1; setLimit (tvState); // setMode (mTextView); new Thread (new TimeThread ()). start (); // timer Thread new Thread (new loadThread ()). start (); // load Thread} Final Handler timeHandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case 1: timeSec ++; String strTime = ""; if (timeSec <60) {// strTime = timeSec + "seconds" under 1 minute ";} else if (timeSec <3600) {// int minutes = timeSec/60; int second = timeSec % 60; strTime = minutes + "min" + second + "Sec";} else {int hours = timeSec/3600; int minutes = (timeSec-ho Urs * 3600)/60; int second = timeSec % 60; strTime = hours + "Hour" + minutes + "Minute" + second + "second";} mTimeCount. setText ("Running Duration:" + strTime); tvWebHour. setText ("device time" + getTime (1) + "Hour" + getTime (2) + "Minute" + getTime (3) + "second");} super. handleMessage (msg) ;}}; public class TimeThread implements Runnable {@ Override public void run () {while (true) {try {Thread. sleep (1000); Message message = new Message (); m Essage. what = 1; timeHandler. sendMessage (message);} catch (Exception e) {e. printStackTrace () ;}}} public void connectWifi () {WifiManager wifiManager = (WifiManager) this. getSystemService (Context. WIFI_SERVICE); if (! WifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (true) ;}} final Handler loadHandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case 1: if (loadMode = 1) {// load if (count <urlArray. length) {webPath = urlIndex + urlArray [count ++];} else {count = 0; webPath = urlIndex + urlArray [count ++];} else if (loadMode = 2) {// load if (count> = urlArray in reverse order. Length) {count = 0;} reverseCount = urlArray. length-1-(count ++); webPath = urlIndex + urlArray [reverseCount];} else {// default: Random Loading count = new Random (). nextInt (urlArray. length); // 0 to (urlArray. length-1) if (getTime (3) % 5 = 0) {// if the remainder of the current number of seconds is 0 for 5, load the last five count = (urlArray. length-5) + new Random (). nextInt (5); // 0 to 4, plus} webPath = urlIndex + urlArray [count];} keepScreenOn (Main. this, true); l OadBlog (webPath); break; case 2: tvState. setText ("Network Error"); keepScreenOn (Main. this, false);/* try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} connectWifi (); */break;} super. handleMessage (msg) ;}}; public class loadThread implements Runnable {@ Override public void run () {while (true) {if (isNetworkConnected (Main. this) {try {webHour = getTime (1 ); If (webHour <0 | webHour> 24) {webHour = 8;} if (webHour> = 23 | webHour <7) {// [) AND) int randomId = 1 + new Random (). nextInt (3); // 1-3 mTimeHint. setText ("latency" + randomId + "Minute"); Thread. sleep (randomId * 60000);} else if (webHour> = 8 & webHour <= 12) | (webHour> = 14 & webHour <= 18 )) {int randomId = 1 + new Random (). nextInt (3); // 1-3 mTimeHint. setText ("latency" + randomId + "Seconds"); Thread. sleep (randomId * 1000);} else {int randomId = 1 + new Random (). nextInt (3); // 1-3 mTimeHint. setText ("latency" + (2 * randomId) + "second"); Thread. sleep (randmid * 2000);} Message message = new Message (); message. what = 1; loadHandler. sendMessage (message);} catch (Exception e) {e. printStackTrace () ;}} else {Message message = new Message (); message. what = 2; loadHandler. sendMessage (messag E) ;}}} public int getTime (int type) {Date date = new Date (); if (type = 1) {return date. getHours ();} else if (type = 2) {return date. getMinutes ();} else if (type = 3) {return date. getSeconds () ;}else {return 0 ;}} private void loadBlog (String path) {mWebView. loadUrl (path); mWebView. setWebViewClient (new WebViewClient () {@ Override public boolean shouldOverrideUrlLoading (WebView vi Ew, String url) {// return super. shouldOverrideUrlLoading (view, url); view. loadUrl (url); return true ;}@ Override public void onPageFinished (WebView view, String url) {// captures the URL's eight-bit ID String subString = url. toString (). substring (url. toString (). length ()-8, url. toString (). length (); super. onPageFinished (view, url); if (! SubString. equals (urlArray [urlArray. length-1]) {// whether the last element of the array is realCount ++; if (loadMode = 2) {tvState. setText ("page" + (reverseCount + 1) + "/" + urlArray. length + "--" + subString + "loaded successfully." + realCount + "page");} else {tvState. setText ("page" + count + "/" + urlArray. length + "--" + subString + "loaded," + realCount + "page");} if (isLimit & (realCount> = limitCount )) {ExitApp (tvState) ;}} Else {realCount ++; if (loadMode = 2) {tvState. setText ("page" + (reverseCount + 1) + "/" + urlArray. length + "--" + subString + "loaded successfully." + realCount + "page");} else {tvState. setText ("page" + count + "/" + urlArray. length + "--" + subString + "loaded, total loaded" + realCount + "page") ;}}@ Override public void onReceivedError (WebView view, int errorCode, string description, String failingUrl ){ Super. onReceivedError (view, errorCode, description, failingUrl); Log. e ("CSDN", "" + errorCode + description); tvState. setText ("loading failed. Please try again later. ") ;}}) ;}public void initModeBtn () {// initial Mode Button Text if (loadMode = 1) {btnChangeMode. setText ("ordered mode");} else if (loadMode = 2) {btnChangeMode. setText ("reverse mode");} else if (loadMode = 3) {btnChangeMode. setText ("random mode") ;}} public void ExitApp (View view) {keepScreenOn (Main. this, false); Intent intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); startActivity (intent); android. OS. process. killProcess (android. OS. process. myPid ();} public void showHide (View view) {if (mWebView. getVisibility () = View. GONE) {mWebView. setVisibility (View. VISIBLE); btnShowHide. s EtText ("hidden");} else if (mWebView. getVisibility () = View. VISIBLE) {mWebView. setVisibility (View. GONE); btnShowHide. setText ("show") ;}} public void setLimit (View view) {if (etLimit. getText (). toString ()! = Null & etLimit. getText (). toString (). trim (). length ()> 0) {limitCount = Integer. parseInt (etLimit. getText (). toString ();} if (limitCount <0) {limitCount = 0;} if (limitCount = 0) {isLimit = false; tvLimit. setText ("restriction rules: unlimited");} else if (limitCount> 0) {isLimit = true; tvLimit. setText ("restriction rule: loading" + limitCount + "Page and exit") ;}} public void changeMode (View view) {if (loadMode = 1) {loadMode = 2; btnChangeMode. setText ("reverse mode");} else if (loadMode = 2) {loadMode = 3; btnChangeMode. setText ("random mode");} else if (loadMode = 3) {loadMode = 1; btnChangeMode. setText ("ordered mode") ;}// network status public boolean isNetworkConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager. getActiveNetworkInfo (); if (mNetworkInfo! = Null) {return mNetworkInfo. isAvailable () ;}} return false;} public static void keepScreenOn (Context context, boolean on) {if (wakeLock = null) {PowerManager pm = (PowerManager) context. getSystemService (Context. POWER_SERVICE); wakeLock = pm. newWakeLock (PowerManager. SCREEN_BRIGHT_WAKE_LOCK | PowerManager. ON_AFTER_RELEASE, "= KeepScreenOn ="); wakeLock. acquire ();} else {Log. e ("ZMS", "WakeLock Already On") ;}} else {if (wakeLock! = Null) {wakeLock. release (); wakeLock = null ;}} public String [] urlArray = new String [82]; // ###### Modify Me ######// ##### Modify Me ##### public void initUrl () {urlArray [81] = "42245481"; // urlArray [80] = "42192861"; // urlArray [79] = "42149731 "; // urlArray [78] = "42127681"; // urlArray [77] = "42110329"; // urlArray [76] = "42085939 "; // urlArray [75] = "42042547"; // urlArray [74] = "42023747"; // urlArray [73] = "42001049 "; // urlArray [72] = "41982749"; // urlArray [71] = "41964147"; // urlArray [70] = "41909621 "; // urlArray [69] = "41898205"; // urlArray [68] = "41852845"; // urlArray [67] = "41807913 "; // urlArray [66] = "41787113"; // urlArray [65] = "41773471"; urlArray [64] = "41751259 "; urlArray [63] = "41674841"; urlArray [62] = "41651909"; urlArray [61] = "41623903"; urlArray [60] = "41593769 "; urlArray [59] = "41577725"; urlArray [58] = "41559167"; urlArray [57] = "41515985"; urlArray [56] = "41493939 "; urlArray [55] = "41452531"; urlArray [54] = "41444931"; urlArray [53] = "41378661"; urlArray [52] = "41310305 "; urlArray [51] = "41276657"; urlArray [50] = "41039497"; urlArray [49] = "41006711"; urlArray [48] = "41006711 "; urlArray [47] = "40897455"; urlArray [46] = "40865095"; urlArray [45] = "40817269"; urlArray [44] = "40779347 "; urlArray [43] = "40737065"; urlArray [42] = "40677441"; urlArray [41] = "40648971"; urlArray [40] = "40614011 "; urlArray [39] = "40580995"; urlArray [38] = "40544365"; urlArray [37] = "40507193"; urlArray [36] = "40423803 "; urlArray [35] = "40227437"; urlArray [34] = "40160843"; urlArray [33] = "40082987"; urlArray [32] = "40051331 "; urlArray [31] = "40021129"; urlArray [30] = "39997481"; urlArray [29] = "39973739"; urlArray [28] = "39936867 "; urlArray [27] = "39897617"; urlArray [26] = "39862237"; urlArray [25] = "39834141"; urlArray [24] = "39777229 "; urlArray [23] = "39759943"; urlArray [22] = "39741221"; urlArray [21] = "39722311"; urlArray [20] = "39700219 "; urlArray [19] = "39674051"; urlArray [18] = "39645901"; urlArray [17] = "39618057"; urlArray [16] = "39585513 "; urlArray [15] = "39558107"; urlArray [14] = "39527977"; urlArray [13] = "39502685"; urlArray [12] = "39480497 "; urlArray [11] = "39454145"; urlArray [10] = "39454145"; urlArray [9] = "39434451"; urlArray [8] = "39401061 "; urlArray [7] = "39377419"; urlArray [6] = "39346957"; urlArray [5] = "39323633"; urlArray [4] = "39296439 "; urlArray [3] = "39274019"; urlArray [2] = "39257041"; urlArray [1] = "39234287"; urlArray [0] = "39210795 ";} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {// ToDo: Setting Menu Toast. makeText (Main. this, "ToDo", Toast. LENGTH_SHORT ). show (); return true;} else if (id = R. id. action_exit) {ExitApp (tvState);} return super. onOptionsItemSelected (item) ;}@ Override protected void onDestroy () {super. onDestroy (); keepScreenOn (Main. this, false) ;}@ Override protected void onResume () {// ToDo: Keep Screen Always On When Running super. onResume ();}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.