package com.cqytjr.util;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager; /**
* app处理工具类
*
* @author chenliang
* @version v1.0
* @date 2014-2-20
*/
public class AppUtil {
/**
* 描述:安装APK
*
* @param context
* the context
* @param file
* apk文件路径
*/
public static void installApk(Context context, File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
} /**
* 描述:卸载程序.
*
* @param context
* the context
* @param packageName
* 要卸载程序的包名
*/
public static void uninstallApk(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
Uri packageURI = Uri.parse("package:" + packageName);
intent.setData(packageURI);
context.startActivity(intent);
} /**
* 获取版本号
*
* @param context
* @return
*/
public static int getVersionCode(Context context) {
try {
return context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
LogUtil.error("获取版本号失败");
e.printStackTrace();
}
return 0;
} /**
* 获取版本名
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
try {
return context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
LogUtil.error("获取版本名失败");
e.printStackTrace();
}
return "";
} /**
* 获取设备的IMEI/设备号
*
* @param context
* @return IMEI/设备号
*/
public static String getIMEI(Activity context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getDeviceId();
} /**
* 保存屏幕常亮
*
* @param context
*/
public static void keepLight(Activity activity) {
// 保持屏幕常亮
activity.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} /**
* 获取本机IP地址
*/
public static String getLocalIpToString() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
} /**
* 获取本机电话号码
*
* @param context
* @return
*/
public static String getPhone_num(Context context) {
String phone = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
return phone;
} /**
* Role:Telecom service providers获取手机服务商信息 <BR>
* 需要加入权限<uses-permission
* android:name="android.permission.READ_PHONE_STATE"/> <BR>
*/
public static String getProvidersName(Context context) {
String ProvidersName = null;
// 返回唯一的用户ID;就是这张卡的编号神马的
String IMSI = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
System.out.println(IMSI);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
return ProvidersName;
} /**
* 获取本机MAC地址
*
* @param context
* @return
*/
public static String getMac(Context context) {
String macAddress = null;
WifiManager wifiMgr = null;
Object obj = context.getSystemService(Context.WIFI_SERVICE);
if (null != obj) {
wifiMgr = (WifiManager) obj;
}
WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
if (null != info) {
macAddress = info.getMacAddress();
}
return macAddress;
}
}

  

android常用函数的更多相关文章

  1. Android常用的物理按键及其触发事件

    Activity和View都能接收触摸和按键,如果响应事件只需要在继承类里复写事件函数即可:当一个视图(如一个按钮)被触摸时,该对象上的 onTouchEvent() 方法会被调用.不过,为了侦听这个 ...

  2. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  3. Android常用开源项目

    Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progre ...

  4. EventBus的其他常用函数

    上一篇EventBus最简易使用方式介绍了EventBus最简易的使用方式,摆脱了叽里呱啦+图片的长篇大论.目的是为了让刚开始接触的人们不晕头转向.那么这篇..我也要开始图片+叽里呱啦了. 转载请注明 ...

  5. php常用函数搜集

    搜集了几个php常用函数方法....相信项目中肯定会用到吧... <?php /** * @param $arr * @param $key_name * @return array * 将数据 ...

  6. Android常用酷炫控件(开源项目)github地址汇总

    转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...

  7. Appium——api常用函数

    appium常用函数介绍:   获取页面信息:   1. def get_current_activity(cls, driver): ''' 获取当前页面的activity :param drive ...

  8. Android 常用炫酷控件(开源项目)git地址汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  9. 性能测试基础-开门篇3(LR常用函数介绍)

    LR常用的函数,协议不一样函数会不一样,这里简单的介绍下HTTP\WEBSERVICE\SOCKET协议常用函数: HTTP: web_set_max_html_param_len("102 ...

随机推荐

  1. 【Window 7】解决Win7远程桌面无法全屏的方法

    在Windows中有一项远程桌面功能,可以登录到其他电脑上进行远程控制,就像操纵本机一样,非常方便.但是最近发现用远程桌面登录过去时并不是全屏模式,而是窗口模式,老要拉滚动条,实在很不方便,今天还是到 ...

  2. jenkins 启动slave时,找不到合适的java程序

    今天添加slave 又不成功,我用右键,发现随意点了java的那个打开,结果刚弹出来就关闭了窗口,显示未启动成功. 真是小白的想法,不过跟工具打交道久了,多少逻辑上想一下不对. 一定是用了不合适的程序 ...

  3. [1]朝花夕拾-JAVA类的执行顺序

    最近在温习java的基础,刷题刷到java的执行顺序,很汗颜,答案回答错了! 题目类似如下: package com.phpdragon.study.base; public class ExecOr ...

  4. Oracle中add_months()函数的用法

    查询当前时间1个月以前的时间: select add_months(sysdate,-1) from dual; 查询当前时间1个月以后的时间: select add_months(sysdate,1 ...

  5. 报错libtest: error while loading shared libraries: libuv.so.1: cannot open shared object file: No such file or directory

    使用g++编译.运行libuv的demo错误解决 我们通过例子来讲述监视器的使用. 例子中空转监视器回调函数被不断地重复调用,  通过例子我们也可以了解到: 由于设置了监视器, 所以调用 uv_run ...

  6. fyzcms---相关文章推荐功能

    在用我写的fyzcms的做优化的时候,考虑到文章之间的权重集中,以及相互文章间的低耦合,所以设计了一个相关文章推荐的功能. 具体使用: <if condition="count($fi ...

  7. solus 系统 - 安装编译工具

    将会安装 gcc,make等工具 sudo eopkg install -c system.devel VirtualBox工具 https://solus-project.com/articles/ ...

  8. ubuntu部署php7.1

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade 安装PHP sudo add-apt-repository ppa:ondrej/php sud ...

  9. Lucene.net(4.8.0) 学习问题记录一:分词器Analyzer的构造和内部成员ReuseStategy

    前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...

  10. BZOJ 3224 - 普通平衡树 - [Treap][Splay]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...