In the article "Android Phone defender-parsing JSON and message mechanism send different types of messages," the message mechanism to send different types of information has not been fully implemented, in the event of an exception, should pop up a toast tip exception, the code is as follows:
PrivateHandler Mhandler =NewHandler () {// Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { Caseupdate_version://Pop-up dialog box prompting user to update//Showupdatedialog (); Break; CaseEnter_home://go to the application main interface, activity jump processEnterhome (); Break; CaseURL_ERROR:ToastUtil.show (Getapplicationcontext (),"URL Exception"); Enterhome (); Break; CaseIO_ERROR:ToastUtil.show (Getapplicationcontext (),"Read Exception"); Enterhome (); Break; CaseJSON_ERROR:ToastUtil.show (Getapplicationcontext (),"JSON parsing exception"); Enterhome (); Break; default: Break; } }; };
Because the pop-up toast operation is more common, so it is written as a tool class for a simple package, easy to call later.
This article address: http://www.cnblogs.com/wuyudong/p/5902161.html, reprint please indicate source address.
Then the new tool class: Toastutil.java
Packagecom.wuyudong.mobilesafe.Utils;ImportAndroid.content.Context;ImportAndroid.widget.Toast; Public classToastutil {//Print Toast /** * @paramContext Environment *@parammessage Print text content*/ Public Static voidShow (context context, String message) {Toast.maketext (context, message,0); }}
If the current version number < server version number, a dialog box prompts for updates
/*** Pop-up dialog box prompting user to update*/ protected voidShowupdatedialog () {System.out.println ("Showupdatedialog"); //dialog box, which is dependent on the presence of the activityBuilder Builder =NewAlertdialog.builder ( This); //set the upper left corner iconBuilder.seticon (R.drawable.ic_launcher); Builder.settitle ("Version Update"); //Setting Description Contentbuilder.setmessage (mversiondes); //Active button, update nowBuilder.setpositivebutton ("Update Now",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //download apk,apk link address, DownloadURL downloadapk (); } }); Builder.setnegativebutton ("Say it later,"NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //Cancel the dialog box and go to the main interfaceEnterhome (); } }); Builder.show (); }
The effect of the implementation is as follows:
where the downloadapk () function is implemented in the following article
Android Phone Defender--Popup dialog box