首页 > 代码库 > Activity常用工具

Activity常用工具

1、 判断当前网络是否通
public static boolean isNetworkAvailable() {
Context context = CrashApplication.getInstance();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;

}

2、 获取当前版本号

public static String getVersionCode() {
PackageInfo pinfo;
String versionCode = "0";
try {
Context context = CrashApplication.getInstance();
pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
versionCode = String.valueOf(pinfo.versionCode);
} catch (Exception e) {}
return versionCode;
}

3、 获取本机手机号码

public static String getPhoneNum() {
Context context = CrashApplication.getInstance();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getLine1Number();
}

4、获取版本名称
public static String getVersionName() {
PackageInfo pinfo;
String versionName = "";
try {
Context context = CrashApplication.getInstance();
pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
versionName = pinfo.versionName;
} catch (Exception e) {}
return versionName;
}

5、得到当前文件存储路径 优先返回SDCARD路径;如无SDCARD则返回手机内存中应用程序数据文件夹路径
public static String getStoragePath() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return "/mnt/sdcard";
} else {
Context context = CrashApplication.getInstance();
return context.getFilesDir().toString();
}
}


6、 判断当前是否有可用的SDCARD
public static boolean isSDCardEabled() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}


7、获取AndroidManifest.xml中的MetaData标签值
public static String getAppMetaData1(String key) {
try {
ApplicationInfo ai = null;
Context ctx = CrashApplication.getInstance();
ai = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
String value = http://www.mamicode.com/ai.metaData.getString(key);
// if (!TextUtils.isEmpty(value)) {
// value = http://www.mamicode.com/value.replaceFirst("game:", "");
// }
Log.i("Context", "value:" + value);
return value;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}


Activity常用工具