Android常用工具類

來源:互聯網
上載者:User

public class CommonUtil {


public static boolean hasSDCard() {


String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}


/**
* 擷取最大記憶體
*
* @return
*/
public static long getMaxMemory() {


return Runtime.getRuntime().maxMemory() / 1024;
}


/**
* 檢查網路
*
* @param context
* @return
*/
public static boolean checkNetState(Context context) {


boolean netstate = false;
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {


NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {


if (info[i].getState() == NetworkInfo.State.CONNECTED) {


netstate = true;
break;
}
}
}
}
return netstate;
}


public static void showToast(Context context, String tip) {


Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
}


public static DisplayMetrics metric = new DisplayMetrics();


/**
* 得到螢幕高度
*
* @param context
* @return
*/
public static int getScreenHeight(Activity context) {


context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}


/**
* 得到螢幕寬度
*
* @param context
* @return
*/
public static int getScreenWidth(Activity context) {


context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}


/**
* 根據手機的解析度從 dp 的單位 轉成為 px(像素)
*/


public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}


/**
* 根據手機的解析度從 px(像素) 的單位 轉成為 dp
*/
public static int px2dip(Context context, float pxValue) {


final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}


/**
* 查詢手機內非系統應用
*
* @param context
* @return
*/
public static List getAllApps(Context context) {


List apps = new ArrayList();
PackageManager pManager = context.getPackageManager();
// 擷取手機內所有應用
List paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判斷是否為非系統預裝的應用程式
if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
// customs applications
apps.add(pak);
}
}
return apps;
}


public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}


/**
* 擷取版本號碼和版本次數
*
* @param context
* @return
*/
public static String getVersionCode(Context context, int type) {


try {


PackageInfo pi = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
if (type == 1) {


return String.valueOf(pi.versionCode);
} else {


return pi.versionName;
}
} catch (NameNotFoundException e) {


e.printStackTrace();
return null;
}
}


// 通過Service的類名來判斷是否啟動某個服務
public static boolean messageServiceIsStart(
List mServiceList,
String className) {


for (int i = 0; i < mServiceList.size(); i++) {
if (className.equals(mServiceList.get(i).service.getClassName())) {


return true;
}
}
return false;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.