Android Common function code block

Source: Internet
Author: User

1, set activity Untitled, Full screen

////

2. Get screen height and width

// Gets the height and width of the screen used to windowmanager this class WindowManager wm =int width = wm.getdefaultdisplay (). getwidth (); int height = wm.getdefaultdisplay (). GetHeight ();

3. Get all kinds of information about mobile phone

Telephonymanager TM = (Telephonymanager) This. Getsystemservice (Context.telephony_service); String IMEI= Tm.getdeviceid ();//Mobile Device International identification codeString IMSI = Tm.getsubscriberid ();//International Mobile Subscriber Identification CodeString Tel = tm.getline1number ();//Phone numberString Model= Android.os.Build.MODEL;//Phone ModelString SDK = Android.os.Build.VERSION.SDK;//SDK versionString release = Android.os.Build.VERSION.RELEASE;//System Version//identify mobile providers based on IMSI numbers Publicstring Getprovidersname (String IMSI) {string Providersname=NULL; //IMSI Front 3 bit 460 is the country, followed by 2 bit 00 02 is China Mobile, 01 is China Unicom, 03 is Chinese telecom.     if(Imsi.startswith ("46000") | | Imsi.startswith ("46002") ) {Providersname= "China Mobile"; } Else if(Imsi.startswith ("46001") ) {Providersname= "China Unicom"; } Else if(Imsi.startswith ("46003") ) {Providersname= "China Telecom"; }    returnProvidersname;}

4. Use Toast to output a string

 Public void showtoast (String text) {    Toast.maketext (this, Text, Toast.length_short). Show ();

5. Write a string into the file

//write a string into the file Public voidWriteFile (String str,string path) {file file;    FileOutputStream out; Try{        //Create a fileFile =NewFile (path);        File.createnewfile (); //Open File output streamout =Newfileoutputstream (file); //converts a string into a byte array to write to a fileOut.write (Str.getbytes ());    Out.close (); }Catch(IOException e) {}}

6. Read the contents of the file to a string

//read the contents of the file to a string Publicstring GetFileInfo (string path) {file file; String Str= "";    FileInputStream in; Try{        //Open the file InputStreamFileNewFile (path); Inch=Newfileinputstream (file); //reads the contents of a file into a byte array        intLength = (int) file.length (); byte[] temp =New byte[length]; In.read (temp,0, length); STR= encodingutils.getstring (temp, "utf-8"));    In.close (); }Catch(IOException e) {}returnstr;}

7, program installation, uninstall, update

//bring up the system installation applicationString fileName = environment.getexternalstoragedirectory () +Apkname; Uri URI= Uri.fromfile (NewFile (FileName)); Intent Intent=NewIntent (Intent.action_view); Intent.setdataandtype (URI,"Application/vnd.android.package-archive"); This. StartActivity (intent);//bring up the system uninstall appUri Packageuri = Uri.parse ("Package:your.app.id"); Intent Intent=NewIntent (intent.action_delete,Packageuri); startactivity (Intent);

8, realize click two times return key exit

//The first step is to define a variable that identifies whether to exitBooleanIsexit;//Step Two, rewrite the onkeydown method in activity@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {    if(KeyCode = =keyevent.keycode_back)        {exit (); return false; } Else {        return Super. OnKeyDown (KeyCode, event); }}//step three, write an exit method Public voidexit () {if(!isexit) {Isexit=true; Toast.maketext (Getapplicationcontext (),"Press one more time to exit the program", Toast.length_short). Show (); Mhandler.sendemptymessagedelayed (0, 2000); } Else{Intent Intent=NewIntent (Intent.action_main);        Intent.addcategory (Intent.category_home);        StartActivity (Intent); System.exit (0); }}//Fourth step, according to the message in the exit () method, write a handlerHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method Stub        Super. Handlemessage (msg); Isexit=false; } };

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.