DeviceUtils
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager; import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.UUID; public class DeviceUtils {
protected static final String TAG = DeviceUtils.class.getSimpleName(); // 移动
private static final int CHINA_MOBILE = 1;
// 联通
private static final int UNICOM = 2;
// 电信
private static final int TELECOMMUNICATIONS = 3;
// 失败
private static final int ERROR = 0; /**
* 手机唯一标识
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
String uniqueId = deviceUuid.toString();
return uniqueId;
} /**
* 手机MAC地址
* @param context
* @return
*/
public static String getMacAddressInfo(Context context) {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
return info.getMacAddress();
} /**
* TelephonyManager对象
* @param context
* @return
*/
private static TelephonyManager getTelphoneManager(Context context) {
return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
} /**
* DeviceId
* @param context
* @return
*/
public static String getDeviceID(Context context) {
return getTelphoneManager(context).getDeviceId();
} /**
* IMSI号
* @param context
* @return
*/
public static String getImis(Context context) {
return getTelphoneManager(context).getSubscriberId();
} /**
* 厂商信息
* @return
*/
public static String getProductInfo() {
return android.os.Build.MODEL;
} /**
* release版本
* @return
*/
public static String getReleaseVersion() {
return android.os.Build.VERSION.RELEASE;
} /**
* SDK_INT 版本
* @return
*/
public static int getSDKVersion() {
return android.os.Build.VERSION.SDK_INT;
} /**
* 手机号码
* @param context
* @return
*/
public static String getPhoneNum(Context context) {
return getTelphoneManager(context).getLine1Number();
} /**
* 当前运营商
* @param context
* @return 返回0 表示失败 1表示为中国移动 2为中国联通 3为中国电信
*/
public static int getProviderName(Context context) {
String IMSI = getImis(context);
if (IMSI == null) {
return ERROR;
}
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
return CHINA_MOBILE;
} else if (IMSI.startsWith("46001")) {
return UNICOM;
} else if (IMSI.startsWith("46003")) {
return TELECOMMUNICATIONS;
}
return ERROR;
} /**
* 手机CPU名字
* @return
*/
public static String getCpuName() {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
// 读取文件CPU信息
fileReader = new FileReader("/pro/cpuinfo");
bufferedReader = new BufferedReader(fileReader);
String string = bufferedReader.readLine();
String[] strings = string.split(":\\s+", 2);
return strings[1];
} catch (FileNotFoundException e) {
Logger.e(TAG, e.getLocalizedMessage());
} catch (IOException e) {
Logger.e(TAG, e.getLocalizedMessage());
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
Logger.e(TAG, e.getLocalizedMessage());
}
}
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
Logger.e(TAG, e.getLocalizedMessage());
}
}
}
return null;
} /**
* 检查程序是否运行
* @param context
* @param packageName
* @return
*/
public static boolean isAppRunning(Context context, String packageName) {
boolean isAppRunning = false;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(packageName) && info.baseActivity.getPackageName().equals(packageName)) {
isAppRunning = true;
// find it, break
break;
}
}
return isAppRunning;
} /**
* 是否在最前面
*
* @param context
* @param packageName
* @return
*/
public static boolean isTopActivity(Context context, String packageName) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0) {
System.out.println("---------------包名-----------" + tasksInfo.get(0).topActivity.getPackageName());
// 应用程序位于堆栈的顶层
if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {
return true;
}
}
return false;
}
}
DeviceUtils的更多相关文章
- 【uwp】浅谈China Daily 中划词翻译的实现
学习uwp开发也有一段时间了,最近上架了一个小应用(China Daily),现在准备将开发中所学到的一些东西拿出来跟大家分享交流一下. 先给出应用的下载链接:China Daily , 感兴趣的童鞋 ...
- 【iOS】屏幕适配之NSLayoutConstraint
前言 如何实现一张图片在iPhone和iPad上显示不同的尺寸,我了解到一般有三种办法:直接手写代码动态添加约束:把NSLayoutConstraint关联到ViewController里再viewD ...
- 【iOS】Alamofire库在iOS7下设置Head无效的问题
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 同样的代码在iOS8下没有问 ...
- Android WebView 开发教程
声明在先:必须在AndroidMainfest.xml 里面声明权限,否则在Java里面编写的所有WebView浏览网页的代码都无法正常使用 <uses-permission android:n ...
- RecyclerView的基本使用
1.布局文件中使用 <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android ...
- Android实用代码七段(四)
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.发送不重复的通知(Notif ...
- Android WebView常见问题及解决方案汇总
Android WebView常见问题解决方案汇总: 就目前而言,如何应对版本的频繁更新呢,又如何灵活多变地展示我们的界面呢,这又涉及到了web app与native app之间孰优孰劣的争论. 于是 ...
- Java判断访问设备为手机、微信、PC工具类
package com.lwj.util; import javax.servlet.http.HttpServletRequest; /** * 判断访问设备为PC或者手机--工具类 * * @de ...
- 基于Retrofit+RxJava的Android分层网络请求框架
目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...
随机推荐
- linux 使用 rz 上传和 sz下载 命令
linux系统 root权限 lrzsz安装包 ①.在线安装-执行命令 yum install lrzsz 离线安装-需要提前准备好安装包 编译安装 root 账号登陆后,依次执行以下命令: tar ...
- linux 下安装 jdk1.7
1.官网 下载jdk7版本 地址: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-jav ...
- 翻译应用将在Win8.1系统中取消下载安装
自Windows8.Windows Phone 7.1和Windows Phone 8受到影响之后,微软又正式宣布停止对翻译应用提供支持服务.Microsoft Translator这款应用将从Win ...
- Objective-C语法总结收集
PART1--详解Objective-C语法快速参考 一.XCode.Objective-C.Cocoa说的是几样东西? 答案:三样东西. XCode:你可以把它看成是一个开发环境,就好像Visual ...
- Idea中Module is not specified解决办法
打开idea,想跑一个类,但是,给我报了一个红叉: 当我点击run的时候,弹出来一个框: “Error:Module not specified” Module 未指定 “这个原因是项目文件夹有修改 ...
- Thread setUncaughtExceptionHandler
setUncaughtExceptionHandler 用于获取线程运行时异常 线程在执行时是不能抛出 checked 异常的,IDE 只会提示你用 try-catch 包裹起来.因此主线程无法直接获 ...
- MySql大小写配置
新安装mysql5.7版本后,linux环境下默认是大小写敏感的.可以在客户端执行以下命令: SHOW VARIABLES LIKE '%case%' 可以看到 lower_case_table_na ...
- .net上传整个文件夹
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- HGOI 20191108 题解
Problem A 新婚快乐 一条路,被$n$个红绿灯划分成$n+1$段,从前到后一次给出每一段的长度$l_i$,每走$1$的长度需要$1$分钟. 一开始所有红绿灯都是绿色的,$g$分钟后所有红绿灯变 ...
- 【线性代数】3-1:向量空间(Space of Vectors)
title: [线性代数]3-1:向量空间(Space of Vectors) categories: Mathematic Linear Algebra keywords: Vectors Spac ...