<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. 第一篇.markdown

    目录 Day 01 1.markdown的使用感受 2.markdown的基本语法 2.1 标题 2.2 加粗 2.3 斜体 2.4 高亮 2.5 上标 2.6 下标 2.7 代码引用(>式) ...

  2. shell脚本编写之Hello World

    Linux下编写第一个Shell脚本 //创建一个.sh文件 vi demo1.sh //按i进入编辑模式 echo "Hello World !" //按esc→shift+:→ ...

  3. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

    # -*- coding:utf-8 -*- china=u'我爱你,中国' #china=china.encode('utf8') file_txt = open("zhongguo.tx ...

  4. Spring入门篇——第4章 Spring Bean装配(下)

    第4章 Spring Bean装配(下) 介绍Bean的注解实现,Autowired注解说明,基于java的容器注解说明,以及Spring对JSR支持的说明 4-1 Spring Bean装配之Bea ...

  5. MyBatis-09-Lombok

    9.Lombok Project Lombok is a java library that automatically plugs into your editor and build tools, ...

  6. docker管理

    查看容器名 [root@docker ~]# docker inspect -f "{{.Name}}" a2f /u1 停止/启动终止状态的容器 [root@docker ~]# ...

  7. MFC 线程启动、暂停、继续、终止

    CWinThread* p_myThread;//创建线程指针 BOOL flag_myThread = FALSE;//是否终止 //头文件中声明(放类内) static UINT MyThread ...

  8. hdu 6021 MG loves string (一道容斥原理神题)(转)

    MG loves string    Accepts: 30    Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. Django基础之Session操作

    1. 创建一个示例 1.1 第一步 首先创建一个django项目,创建app01, 连接数据库,做好准备工作. 然后在templates目录下创建两个html: login.html负责登录页面: b ...

  10. Java线程优先级及守护线程(二)

    简述 在操作系统中,线程是可以划分优先级的,优先级较高的线程,得到CPU优先执行的几率就较高一些.设置线程的优先级,有助于帮助线程规划期选择下一个哪一个线程优先执行,但是线程优先级高不代表一定会优先执 ...