1.通过反射获取系统属性
/**
* Get the value for the given key.
*
* @return an empty string if the key isn't found
*/
public static String getSystemProperties(Context context, String key) {
String ret = "";
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class systemProperty = cl.loadClass("android.os.SystemProperties");
// Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
Method get = systemProperty.getMethod("get", paramTypes);
// Parameters
Object[] params = new Object[1];
params[0] = new String(key);
ret = (String) get.invoke(systemProperty, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = "";
}
return ret;
}
2.获取屏幕分辨率及dp,px转换
package com.example.androidtest.util;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
/*
* projectName: AndroidTest
* moduleName: DisplayUtils.java
*
* @author chadm.xiang
* @version 1.0.0
* @time 2015-5-19 下午2:04:29
* @Copyright © 2014 Inc.
*/
public class DisplayUtils {
/**
* return the screen width with px,{@link DisplayMetrics#widthPixels}
*
* @param context
* @return screen width
*/
public static int getScreenWidth(Context context) {
DisplayMetrics outDisplayMetrics = context.getResources().getDisplayMetrics();
return outDisplayMetrics.widthPixels;
}
/**
* return the screen height with px,{@link DisplayMetrics#heightPixels}
*
* @param context
* @return height with px
*/
public static int getScreenHeight(Context context) {
DisplayMetrics outDisplayMetrics = context.getResources().getDisplayMetrics();
return outDisplayMetrics.heightPixels;
}
/**
* return the screen density ,{@link DisplayMetrics#density}
*
* @param context
* @return
*/
public static float getScreenDensity(Context context) {
DisplayMetrics outDisplayMetrics = context.getResources().getDisplayMetrics();
return outDisplayMetrics.density;
}
/**
* change dp value to px value
*
* @param context
* @param dipValue The dp value
* @return px value
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* change px value to dp value
*
* @param context
* @param pxValue px value
* @return dp value
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* set Activity window gravity position,{@link Window#setGravity(int)}
*
* @param context
* @param gravity {@link Gravity#BOTTOM} {@link Gravity#TOP}
* {@link Gravity#CENTER}
*/
public static void setGravity(Activity context, int gravity) {
Window window = context.getWindow();
window.setGravity(gravity);
}
}
3.根据Uri获取文件路径
import com.example.androidtest.util.Utils;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
/*
* projectName: AndroidTest
* moduleName: GetPathFromUri4kitkat.java
*
* @author chadm.xiang
* @version 1.0.0
* @time 2015-7-2 下午3:39:16
* @Copyright © 2014 Inc.
*/
public class GetPathFromUri4kitkat {
/**
* get the file path from the given uri
*
* @param context The Context objecct
* @param uri The target uri
* @return The uri file path
*/
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
// MediaProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// MediaStore (and general)
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
// File
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}