码迷,mamicode.com
首页 > 其他好文 > 详细

Activity常用工具

时间:2014-09-20 10:03:07      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:acitvity工具

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 = ai.metaData.getString(key);
// if (!TextUtils.isEmpty(value)) {
// value = value.replaceFirst("game:", "");
// }
Log.i("Context", "value:" + value);
return value;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}


Activity常用工具

标签:acitvity工具

原文地址:http://blog.csdn.net/u012408019/article/details/39429527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!