当前位置: 代码迷 >> 综合 >> Android 常用工具
  详细解决方案

Android 常用工具

热度:75   发布时间:2023-11-22 19:29:13.0

//获取手机型号

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;
}
//判断wifi是否可用 

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是否打开

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;}
//判断移动网络是否可用

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;
}
//判断网络是否可用

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;
}

//判断是否第一次启动App

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;
}
//获得当前包的版本号

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;
}
文章来源于https://www.jianshu.com/p/cd7843d705e9

  相关解决方案