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).内存占用少.代码量小以及 ...
随机推荐
- python totp代码
import time import datetime import math import hmac import base64 import qrcode from PIL import Imag ...
- bug-- java.lang.RuntimeException: Type “Klass*"
使用jinfo查看jvm进程id为27523的信息 [java@xftest0 ~]$ jinfo 27523 Attaching to process ID 27523, please wa ...
- 【转载】SELENIUM2支持无界面操作(HTMLUNIT和PHANTOMJS)
SELENIUM2支持无界面操作(HTMLUNIT和PHANTOMJS) selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaD ...
- python_三元运算符
三元运算又称三目运算,是对简单的条件语句的简写 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 else 2 举例: a ...
- NPM酷库:jsdom,纯JS实现的DOM
NPM酷库,每天两分钟,了解一个流行NPM库. 昨天认识了一个在Node.js环境下操作HTML的库 cheerio,cheerio实现了jQuery接口,用起来十分方便.为什么不直接用jQuery呢 ...
- sql random string
begindeclare chars_str varchar(62) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123 ...
- json.loads 报错 json.decoder.JSONDecodeError
json.loads报json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes 出现这个错误其实只 ...
- MySQL主从同步、读写分离配置步骤、问题解决笔记
MySQL主从同步.读写分离配置步骤.问题解决笔记 根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记: 现在使用的两台服务器已经 ...
- Linux初始化脚本
以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行.脚本结合各位大牛一些参数,已经在CentOS 5下通过. 使用方法:将其复制,保存为一个shell文件,比如init.sh ...
- Bootstrap-轮播图-No.7
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...