<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. Async 配置线程池

    需要注意一下 ThreadPoolTaskExecutor  和 Executor  区别 @Configuration public class ExecutorConfig { /** Set t ...

  2. Flutter——Row组件(水平布局组件)

    Row组件的常用属性 属性 说明 mainAxisAlignment 主轴的排序方式 crossAxisAlignment 次轴的排序方式 children 组件子元素 import 'package ...

  3. MySQL开启binlog无法启动ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue

    1 详细异常 ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue Oct 10 21:2 ...

  4. 九:MVC主从表数据加载

    EF对关联表数据加载的三种方式: 延迟加载:只有在需要的时候加载数据.EF默认的加载方式. 贪婪加载:一次性组织好数据,全部加载到内存中. 显式加载:需要通过代码手动加载关联表. 延迟加载 virtu ...

  5. (初级篇)docker基础应用--01

    命令 获取镜像: docker pull //: ,如:docker pull nginx 查看镜像列表: docker images 查看镜像信息: docker inspect

  6. xcode 中 vary for traits详解

    https://www.jianshu.com/p/d6896437e5a7  这篇文章写的很好!

  7. Django 语法笔记

    Django 语法 创建项目框架 django-admin startproject 项目名 创建子app 业务分化,可以优化团队合作,可以明确找锅 python manage.py startapp ...

  8. Xftp6 和 Xshell 6 下载与安装使用

    官网地址:https://www.netsarang.com/zh/all-downloads/ 然后进行安装吧,基本没有什么配置. 打开 xftp 连接 Linux,进行文件操作. 然后 名称:随意 ...

  9. 通俗理解数字签名,ssl数字证书和https

    前言 最近在开发关于PDF合同文档电子签章的功能,大概意思就是在一份PDF合同上签名,盖章,使其具有法律效应.签章有法律效应必须满足两个条件: 能够证明签名,盖章者是谁,无法抵赖 PDF合同在签章后不 ...

  10. whatis

    whatis 查看命令的说明文档位置及说明 是从数据库中取查找 手动更新数据库 并不是每个命令都有,