標籤:android
1、讀取手機連絡人資訊
一般用在讀取手機通訊錄上傳,這一塊比較多。
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;/** * 讀取手機連絡人資訊 */public class ReadPhoneContactUtil {/** * 通過內容觀察者,或者手機連絡人相關的資訊 **/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 };// 擷取手機連絡人Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);if (phoneCursor != null) {while (phoneCursor.moveToNext()) {// 得到手機號碼String phoneNumber = phoneCursor.getString(0);// 當手機號碼為空白的或者為空白欄位 跳過當前迴圈if (TextUtils.isEmpty(phoneNumber))continue;// 得到連絡人名稱String contactName = phoneCursor.getString(1);phoneContacts.add(new PhoneContact(contactName,phoneNumber));}}return phoneContacts;}}
2、通過資源的名稱來擷取資源id
這個最常用的是在國際化或者一鍵修改主題這一塊,根據不同的語言環境,切換不同的字串標識。根據不同的檔案名稱,切換不同的主題
package net.tianyouwang.utils;import android.content.Context;/** * 通過欄位名稱來動態擷取資源的id,再來使用。此類,可以通過欄位名稱動態擷取id。 */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、SDCARD狀態和SDCARD剩餘容量的操作
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的剩餘容量判斷操作 * */public class SDCardAvailableUtils {private static final int imageSize = 1*1024*1024;//1M public static boolean isExistSDCard() { if (android.os.Environment.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; } } } } } /** * 擷取儲存卡的剩餘容量,單位為位元組 * * @param filePath * @return availableSpare */ public static long getAvailableStore(String filePath) { // 取得sdcard檔案路徑 StatFs statFs = new StatFs(filePath); // 擷取block的SIZE long blocSize = statFs.getBlockSize(); // 擷取BLOCK數量 long totalBlocks = statFs.getBlockCount(); // 可使用的Block的數量 long availaBlock = statFs.getAvailableBlocks(); long total = totalBlocks * blocSize;//總的儲存空間 long availableSpare = availaBlock * blocSize;//當前可用儲存空間 return availableSpare; }}