android 常用工具類方法
// 獲取手機(jī)型號(hào)
public static String getInfo() {
Build bd = new Build();
String model = bd.MODEL;
return model;
}
/**
- 獲得屏幕尺寸大小
- */
public static Size getWindowSize(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Size winSize = new Size(wm.getDefaultDisplay().getWidth(), wm
.getDefaultDisplay().getHeight());
return winSize;
}
/**
- 獲得當(dāng)前系統(tǒng)年月日
- */
public static String getTime() {
Time time = new Time();
time.setToNow();
return time.year+"-"+(time.month+1)+"-"+time.monthDay+"-"+time.hour;
}
/**
- 獲取本機(jī)串號(hào)imei
- */
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
/**
- 判斷SD卡是否存在
- */
public static boolean ExistSDCard() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
} else
return false;
}
/**
- SD卡剩余空間
- */
public static long getSDFreeSize()
{
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
//獲得單個(gè)數(shù)據(jù)塊的大小
long blocksize = statfs.getBlockSize();
//獲得空閑數(shù)據(jù)塊的個(gè)數(shù)
long freeblock = statfs.getAvailableBlocks();
return (freeblock*blocksize)/1024/1024; //單位MB
}
/**
- SD卡總?cè)萘?/li>
**/
public static long getSDALLsize()
{
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
//獲得單個(gè)數(shù)據(jù)塊的大小
long blocksize = statfs.getBlockSize();
//獲得全部數(shù)據(jù)塊的個(gè)數(shù)
long allBlock = statfs.getBlockCount();
return (allBlock*blocksize)/1024/1024; //單位MB
}
/**
- 判斷文件夾是否存在,不存在就創(chuàng)建文件目錄SD/wlcache
- return
**/
public static boolean ExistFile()
{
File file = new File(sd_card);
if (!file.exists()) {
file.mkdir();
return true;
}
return true;
}
/**
- 判斷wifi是否可用 turn 是 false 否
- return
**/
public static boolean WifiAvailable(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
return true;
}
return false;
}
/**
- 判斷GPS是否打開
- return
**/
public static boolean ExistGPS(Context context)
{
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
return true;
}
return false;
}
/**
- 判斷移動(dòng)網(wǎng)絡(luò)是否可用
- return
**/
public static boolean MobileAvailable(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
return true;
}
return false;
}
/**
- 判斷網(wǎng)絡(luò)是否可用
- return
**/
public static boolean NetAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null) {
return true;
}
return false;
}
/**
- 判斷apk是否是第一次啟動(dòng)
- return
**/
public static boolean APKFirstSt(Context context)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean firstTime = prefs.getBoolean("com.vieworld.function", true);
if (firstTime){
Editor pEdit = prefs.edit();
pEdit.putBoolean("com.vieworld.function", false);
pEdit.commit();
}
return firstTime;
}
/**
- 獲得當(dāng)前包的版本號(hào)
- return
**/
public static String ApkVersionCode(Context context)
{
PackageManager pack = context.getPackageManager();
PackageInfo packageInfo = null;
String versionCode = null;
try {
packageInfo = pack.getPackageInfo(context.getPackageName(), 0);
versionCode = packageInfo.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return versionCode;
}
/**
- 判斷apk是否需要更新
**/
public static boolean IsApkUpdata(String versionCode,String newversionCode)
{
System.out.println(versionCode+"="+newversionCode);
if(newversionCode.equals(versionCode ))
{
return true;
}
else
return false;
}