android 项目常用工具方法
博客专区 > luhang 的博客 > 博客详情
android 项目常用工具方法
luhang 发表于1年前
android 项目常用工具方法
  • 发表于 1年前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

public class DeviceInfo {
private static Application APP = null;
private static String sd_card = Environment.getExternalStorageDirectory()+File.separator+"wlcache";
public static Application getApp() {
return APP;
}


// 获取手机型号
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;
}


/**
* 获得当前 系统 年月日

* */
public static String getTime() {
Time time = new Time();
time.setToNow(); 
return time.year+"-"+(time.month+1)+"-"+time.monthDay+"-"+time.hour;
}
/**
* 获取本机串号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());
//获得单个数据块的大小
long blocksize = statfs.getBlockSize();
//获得空闲数据块的个数
long freeblock = statfs.getAvailableBlocks();
return (freeblock*blocksize)/1024/1024; //单位MB

}
/**
* SD卡总容量

**/
public static long getSDALLsize()
{
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
//获得单个数据块的大小
long blocksize = statfs.getBlockSize();
//获得全部数据块的个数
long allBlock = statfs.getBlockCount();
return (allBlock*blocksize)/1024/1024; //单位MB
}


/**
* 判断文件夹是否存在,不存在就创建文件目录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;

}
/**
* 判断移动网络是否可用
* 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;
}
/**
* 判断网络是否可用
* 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是否是第一次启动
* 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;
}
/**
* 获得当前包的版本号
* 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;
}
}
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 15
码字总数 571
×
luhang
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: