Android (12) Android Common tool class.

Source: Internet
Author: User

1, sharedpreferences Save the password file

Package Com.itheima62.mobileguard.utils;import Android.content.context;import android.content.SharedPreferences; Public classSpTools { Public Static voidputstring (Context context,string key,string value) {Sharedpreferences SP=context.getsharedpreferences (Myconstants.spfile, context.mode_private); Sp.edit (). putstring (key, value). commit ();//Save Data    }         Public StaticString getString (Context context,string key,string defvalue) {sharedpreferences SP=context.getsharedpreferences (Myconstants.spfile, context.mode_private); returnsp.getstring (key, defvalue); }}

2, MD5 encryption

Package Com.itheima62.mobileguard.activities;import Java.security.messagedigest;import java.security.NoSuchAlgorithmException; Public classMd5utils { Public Staticstring MD5 (String str) {StringBuilder mess=NewStringBuilder (); Try {            //get the MD5 encryption deviceMessageDigest MD = Messagedigest.getinstance ("MD5"); byte[] bytes =str.getbytes (); byte[] Digest =md.digest (bytes);  for(byteb:digest) {                //turn each byte into a 16 binary number                intD = B &0xFF;//0x000000ffString hexstring =integer.tohexstring (d); if(hexstring.length () = =1) {//the high 4 bits of bytes are 0HexString ="0"+hexstring; } mess.append (hexstring);//The 2-bit hexadecimal number corresponding to each byte is stitched together as a string                            }        } Catch(nosuchalgorithmexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnMess +""; }}
MD5

Android (12) Android Common tool class.

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.