<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的更多相关文章

  1. 【uwp】浅谈China Daily 中划词翻译的实现

    学习uwp开发也有一段时间了,最近上架了一个小应用(China Daily),现在准备将开发中所学到的一些东西拿出来跟大家分享交流一下. 先给出应用的下载链接:China Daily , 感兴趣的童鞋 ...

  2. 【iOS】屏幕适配之NSLayoutConstraint

    前言 如何实现一张图片在iPhone和iPad上显示不同的尺寸,我了解到一般有三种办法:直接手写代码动态添加约束:把NSLayoutConstraint关联到ViewController里再viewD ...

  3. 【iOS】Alamofire库在iOS7下设置Head无效的问题

    声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 同样的代码在iOS8下没有问 ...

  4. Android WebView 开发教程

    声明在先:必须在AndroidMainfest.xml 里面声明权限,否则在Java里面编写的所有WebView浏览网页的代码都无法正常使用 <uses-permission android:n ...

  5. RecyclerView的基本使用

    1.布局文件中使用 <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android ...

  6. Android实用代码七段(四)

    声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.发送不重复的通知(Notif ...

  7. Android WebView常见问题及解决方案汇总

    Android WebView常见问题解决方案汇总: 就目前而言,如何应对版本的频繁更新呢,又如何灵活多变地展示我们的界面呢,这又涉及到了web app与native app之间孰优孰劣的争论. 于是 ...

  8. Java判断访问设备为手机、微信、PC工具类

    package com.lwj.util; import javax.servlet.http.HttpServletRequest; /** * 判断访问设备为PC或者手机--工具类 * * @de ...

  9. 基于Retrofit+RxJava的Android分层网络请求框架

    目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...

随机推荐

  1. 【python】python _、__、__xx__之间的差别

    本文来自 yzl11 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yzl11/article/details/53792416?utm_source=copy 单下 ...

  2. Java中Static关键字详解以及静态变量和成员变量的区别

    一.static关键字的特点 (1)修饰成员变量.成员方法(2)随着类的加载而加载(3)优先于对象存在(4)被所有对象共享(5)可以通过类名调用 它本身也可以通过对象名调用 例如:main()方法由j ...

  3. git 获得当前分支名及其对应的远程分支

    git 获得当前分支名及其对应的远程分支 git symbolic-ref -q --short HEADgit rev-parse --abbrev-ref --symbolic-full-name ...

  4. 使用PyInstaller将.py文件打包并生成Windows下可执行的.exe文件

    最近要使用Qt写一个简单的GUI程序,因此使用了PyQt5来加快开发,使用PyQt5生成可执行的程序时,在Windows操作系统下可以使用pyinstaller库将.py文件及其相关依赖生成为.exe ...

  5. python: json模块 --JSON编码和解码

    json 源代码: Lib/json/__init__.py json.dump() import json numbers = [1, 2, 3, 4] with open('linshi.py', ...

  6. 这个是自定义的代码块在xcode中的路径

    ~/Library/Developer/Xcode/UserData/CodeSnippets

  7. 利用python deque的extend特性实现列表元素查重

    from collections import deque mydquene = deque() mylist = [0,1,1,2,2,3,3,3,3,4,5,6,7,7,8,8,9,10,10,1 ...

  8. C# ado.net 使用task和await(四)

    class Program { private static string constr = "server=.;database=northwnd;integrated security= ...

  9. Spring MVC ajax提交方式

    使用jquery的ajax的方式来提交 第一种,以json对象的形式提交 var jsonData = { "name" : "zhangsan", " ...

  10. Hibernate 5 升级后 getProperties 错误

    升级到 Hibernate 5 后,提示有错误: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/uti ...