Tools commonly used in Android 02

Source: Internet
Author: User

1. Read Phone contact information

Generally used in reading phone address Book upload, this piece more.

Import Java.util.arraylist;import Java.util.list;import Android.content.contentresolver;import Android.content.context;import Android.database.cursor;import Android.provider.contactscontract.commondatakinds.phone;import Android.text.textutils;import com.iss.starwish.bean.phonecontact;/** * Read Phone contact information */public class Readphonecontactutil {/** * through Content Viewer, or mobile contact information **/ public static list<phonecontact> getphonecontacts (Context mcontext) {list<phonecontact> phonecontacts = New Arraylist<phonecontact> (); Contentresolver resolver = Mcontext.getcontentresolver (); string[] phones_projection = new string[] {phone.number,phone.display_name};//get phone contact cursor Phonecursor = Resolver.que RY (phone.content_uri,phones_projection, NULL, NULL, NULL), if (phonecursor! = null) {while (Phonecursor.movetonext ()) {/ /Get Mobile number string phonenumber = phonecursor.getstring (0);//When the mobile number is empty or the empty field skips the current loop if (Textutils.isempty (PhoneNumber)) continue;//get Contact name string contactName = phonecursor.getstring (1);p hoNecontacts.add (New Phonecontact (Contactname,phonenumber));}} return phonecontacts;}}

2. Get the resource ID by the name of the resource

This is most commonly used in internationalization or one-click to modify the theme of this piece, depending on the language environment, switch different string identifiers. Switch between different themes depending on the file name

Package Net.tianyouwang.utils;import android.content.context;/** * Use the field name to dynamically get the ID of the resource. In this class, the ID can be obtained dynamically through the field name. */public class Resourceutil {public static int Getlayoutid (context context, String paramstring) {return cont     Ext.getresources (). Getidentifier (paramstring, "Layout", Context.getpackagename ()); public static int Getstringid (context context, String paramstring) {return context.getresources (). Getidenti     Fier (paramstring, "string", Context.getpackagename ()); public static int Getdrawableid (context context, String paramstring) {return context.getresources (). Getiden     Tifier (paramstring, "drawable", Context.getpackagename ()); public static int Getstyleid (context context, String paramstring) {return context.getresources (). getidentif     IER (paramstring, "style", Context.getpackagename ());     } public static int getId (context context, String paramstring) {    Return Context.getresources (). Getidentifier (paramstring, "id", Context.getpackagename ()); public static int Getcolorid (context context, String paramstring) {return context.getresources (). getidentif     IER (paramstring, "Color", context.getpackagename ()); public static int Getanimationid (context context, String paramstring) {return context.getresources (). Get     Identifier (paramstring, "Anim", Context.getpackagename ()); public static int Getbooleanid (context context, String paramstring) {return context.getresources (). getId     Entifier (paramstring, "bool", Context.getpackagename ()); public static int Getarrayid (context context, String paramstring) {return context.getresources (). Getiden     Tifier (paramstring, "array", Context.getpackagename ()); }}

3. Operation of SDcard state and sdcard remaining capacity

Import Java.io.file;import java.io.fileoutputstream;import java.io.ioexception;import android.os.Environment; Import Android.os.statfs;import android.text.textutils;/*** * SDcard remaining capacity judgment operation * */public class Sdcardavailableutils { private static final int imageSize = 1*1024*1024;//1m public static Boolean Isexistsdcard () {if (ANDROID.OS.ENV Ironment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED) &&        Getavailablestore (Environment.getexternalstoragedirectory (). GetPath ()) > ImageSize) {return true;     } else return false; } public static void Savedatatolocalfile (string data, String FilePath) {if (! Textutils.isempty (data) &&!            Textutils.isempty (FilePath)) {File File = new file (FilePath);            FileOutputStream outstr = null;                try {if (!file.exists ()) {file.createnewfile (); } OUTstr = new FileOutputStream (file);                    if (outstr! = null) {Outstr.write (Data.getbytes ());                Outstr.flush ();            }} catch (IOException e) {e.printstacktrace ();                    } finally {if (outstr! = null) {try {outstr.close ();                    } catch (IOException e) {e.printstacktrace ();                    } finally {outstr = null; }}}}}/** * Get the remaining capacity of the memory card in bytes * * @param filePath * @return Ava Ilablespare */public static long Getavailablestore (String filePath) {//Get SDcard file path StatFs StatFs        = new StatFs (FilePath);        Gets the size of block long blocsize = Statfs.getblocksize ();        Get block number Long totalblocks = Statfs.getblockcount (); The number of blocks that can be used long availablocK = Statfs.getavailableblocks (); Long total = Totalblocks * blocsize;//Overall storage space long Availablespare = Availablock * blocsize;//currently available storage space return    Availablespare; }}



Related Article

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.