Android mobile guard-pop-up dialog box, android guard
In the article "Android mobile guard-Parse json and message mechanisms to send different types of messages", message mechanisms have not yet been fully implemented to send different types of information. In the case of exceptions, the following code prompts an exception in the toast prompt:
Private Handler mHandler = new Handler () {// public void handleMessage (android. OS. message msg) {switch (msg. what) {case UPDATE_VERSION: // the pop-up dialog box prompts the user to update // showUpdateDialog (); break; case ENTER_HOME: // enter the main interface of the application. enterHome (); break; case URL_ERROR: ToastUtil. show (getApplicationContext (), "url exception"); enterHome (); break; case IO_ERROR: ToastUtil. show (getApplicationContext (), "Read exception"); enterHome (); break; case JSON_ERROR: ToastUtil. show (getApplicationContext (), "json parsing exception"); enterHome (); break; default: break ;}};};
As the pop-up toast operation is common, it is written as a tool class for simple encapsulation, so that it can be called later.
URL: http://www.cnblogs.com/wuyudong/p/5902161.html.
Create a tool class: ToastUtil. java
Package com. wuyudong. mobilesafe. utils; import android. content. context; import android. widget. toast; public class ToastUtil {// print Toast/*** @ param context Context * @ param message print text content */public static void show (context, String message) {Toast. makeText (context, message, 0 );}}
If the current version number is <server version number, an update prompt is displayed in the dialog box.
/*** Dialog box appears, prompting you to update */protected void showUpdateDialog () {System. out. println ("showUpdateDialog"); // the dialog box depends on the Builder builder = new AlertDialog of the activity. builder (this); // set the icon builder in the upper left corner. setIcon (R. drawable. ic_launcher); builder. setTitle ("version Update"); // sets the description content builder. setMessage (mVersionDes); // click the active button to update the builder immediately. setPositiveButton ("update now", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// download apk, apk URL, downloadUrl downloadApk () ;}}); builder. setNegativeButton ("later", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// cancel dialog box, enter enterHome ();}}); builder. show ();}
The implementation result is as follows:
The downloadApk () function is implemented in the next article.