Commonly used tools in Android 02

Source: Internet
Author: User

1. Read mobile phone contact information

It is generally used to read and upload the address book on a mobile phone.

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 mobile phone contact information */public class ReadPhoneContactUtil {/*** use content observer or mobile phone contact information **/public static List
 
  
GetPhoneContacts (Context mContext) {List
  
   
PhoneContacts = new ArrayList
   
    
(); ContentResolver resolver = mContext. getContentResolver (); String [] PHONES_PROJECTION = new String [] {Phone. NUMBER, Phone. DISPLAY_NAME}; // obtain the mobile phone contact Cursor phoneCursor = resolver. query (Phone. CONTENT_URI, PHONES_PROJECTION, null); if (phoneCursor! = Null) {while (phoneCursor. moveToNext () {// obtain the mobile phone number String phoneNumber = phoneCursor. getString (0); // when the mobile phone number is empty or empty, the current loop if (TextUtils. isEmpty (phoneNumber) continue; // obtain the contact name String contactName = phoneCursor. getString (1); phoneContacts. add (new PhoneContact (contactName, phoneNumber) ;}return phoneContacts ;}}
   
  
 

2. Obtain the resource id by the Resource Name

This is most commonly used in international or one-click topic modification. Different string identifiers are switched based on different language environments. Different topics are switched based on different file names.

Package net. tianyouwang. utils; import android. content. Context;/*** use the field name to dynamically obtain the resource id. This class can be dynamically obtained through the field name. */Public class ResourceUtil {public static int getLayoutId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "layout", context. getPackageName ();} public static int getStringId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "string", context. getPackageName ();} public static int getDrawableId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "drawable", context. getPackageName ();} public static int getStyleId (Context context, String paramString) {return context. getResources (). getIdentifier (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 (). getIdentifier (paramString, "color", context. getPackageName ();} public static int getAnimationId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "anim", context. getPackageName ();} public static int getBooleanId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "bool", context. getPackageName ();} public static int getArrayId (Context context, String paramString) {return context. getResources (). getIdentifier (paramString, "array", context. getPackageName ());}}

3. Operations on SDCARD status and remaining SDCARD 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;/*** determine the remaining capacity of the SDCard **/public class SDCardAvailableUtils {private static final int imageSize = 1*1024*1024; // 1 M public static boolean isExistSDCard () {if (android. OS. environment. getExternalStorageState (). equals (android. OS. environme Nt. 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 memory capacity, the Unit is byte ** @ param filePath * @ return availableSpare */public static long getAvailableStore (String filePath) {// get the path of the sdcard file StatFs statFs = new StatFs (filePath ); // obtain the block SIZE long blocSize = statFs. getBlockSize (); // obtain the number of blocks long totalBlocks = statFs. getBlockCount (); // The number of blocks that can be used long availaBlock = statFs. getAvailableBlocks (); long total = totalBlocks * blocSize; // total storage space long availableSpare = availaBlock * blocSize; // 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.