Android手机管理工具类详解
android手机管理工具类 appphonemgr分享给大家,供大家参考,具体内容如下
工具类内的方法:
getinstance : 单例对象
getsdkversionnumber : 获取手机系统版本号
getphonemodel : 获取手机型号
getphonewidth : 获取手机宽度
getphoneheight : 获取手机高度
getphoneimei : 获取手机imei串号 ,gsm手机的 imei 和 cdma手机的 meid
getphonesim : 获取手机sim卡号
getphonenum : 获取手机号
issdcardmount : 判断sd卡是否挂载
getsdfreesize : 获取sd卡剩余空间的大小
getsdallsize : 获取sd卡空间的总大小
istablet : 判断是否是平板
isapkinstalled : 判断一个apk是否安装
getapppermissions : 获取应用权限 名称列表
getinstalledapp : 获取手机内安装的应用
getuserinstalledapp : 获取手机安装非系统应用
getinstalledappinfo : 获取安装应用的信息
startapppkg : 打开指定包名的应用
uninstallapk : 卸载指定包名的应用
callphone : 直接呼叫指定的号码
tocallphoneactivity : 跳转至拨号界面
sendmessage : 直接调用短信api发送信息(设置监听发送和接收状态)
tosendmessageactivity: 跳转至发送短信界面(自动设置接收方的号码)
appphonemgr类的代码
package com.example.phone; import android.content.context; import android.content.intent; import android.content.pm.applicationinfo; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.content.pm.packagemanager.namenotfoundexception; import android.content.res.configuration; import android.net.uri; import android.os.environment; import android.os.statfs; import android.provider.contacts; import android.telephony.telephonymanager; import android.view.windowmanager; import java.io.file; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import java.util.regex.matcher; import java.util.regex.pattern; /** * 主要功能:手机管理工具类 */ public class appphonemgr { private static appphonemgr phoneutil; public static appphonemgr getinstance() { if (phoneutil == null) { synchronized (appphonemgr.class) { if (phoneutil == null) { phoneutil = new appphonemgr(); } } } return phoneutil; } /** * 获取手机系统版本号 * * @return */ public integer getsdkversionnumber() { integer sdkversion; try { sdkversion = integer.valueof(android.os.build.version.sdk_int); } catch (numberformatexception e) { e.printstacktrace(); sdkversion = integer.valueof(0); } return sdkversion; } /** * 获取手机型号 */ public string getphonemodel() { return android.os.build.model; } /** * 获取手机宽度 */ @suppresswarnings("deprecation") public int getphonewidth(context context) { windowmanager wm = (windowmanager) context .getsystemservice(context.window_service); return wm.getdefaultdisplay().getwidth(); } /** * 获取手机高度 * * @param context */ @suppresswarnings("deprecation") public int getphoneheight(context context) { windowmanager wm = (windowmanager) context .getsystemservice(context.window_service); return wm.getdefaultdisplay().getheight(); } /** * 获取手机imei串号 ,gsm手机的 imei 和 cdma手机的 meid. * * @param context */ public string getphoneimei(context context) { telephonymanager tm = (telephonymanager) context .getsystemservice(context.telephony_service); if (tm == null) return null; return tm.getdeviceid(); } /** * 获取手机sim卡号 * * @param context */ public string getphonesim(context context) { telephonymanager tm = (telephonymanager) context .getsystemservice(context.telephony_service); if (tm == null) return null; return tm.getsubscriberid(); } /** * 获取手机号 * * @param context */ public string getphonenum(context context) { telephonymanager tm = (telephonymanager) context .getsystemservice(context.telephony_service); if (tm == null) return null; return tm.getline1number(); } /** * 判断sd卡是否挂载 */ public boolean issdcardmount() { if (environment.getexternalstoragestate().equals( environment.media_mounted)) { return true; } else { return false; } } /** * 获取sd卡剩余空间的大小 */ @suppresswarnings("deprecation") public long getsdfreesize() { file path = environment.getexternalstoragedirectory(); // 取得sd卡文件路径 statfs sf = new statfs(path.getpath()); long blocksize = sf.getblocksize(); // 获取单个数据块的大小(byte) long freeblocks = sf.getavailableblocks();// 空闲的数据块的数量 // 返回sd卡空闲大小 return (freeblocks * blocksize) / 1024 / 1024; // 单位mb } /** * 获取sd卡空间的总大小 */ @suppresswarnings("deprecation") public long getsdallsize() { file path = environment.getexternalstoragedirectory(); // 取得sd卡文件路径 statfs sf = new statfs(path.getpath()); long blocksize = sf.getblocksize(); // 获取单个数据块的大小(byte) long allblocks = sf.getblockcount(); // 获取所有数据块数 // 返回sd卡大小 return (allblocks * blocksize) / 1024 / 1024; // 单位mb } /** * 判断是否是平板 */ public boolean istablet(context context) { return (context.getresources().getconfiguration().screenlayout & configuration.screenlayout_size_mask) >= configuration.screenlayout_size_large; } /** * 判断一个apk是否安装 * * @param context * @param packagename */ public boolean isapkinstalled(context context, string packagename) { packagemanager pm = context.getpackagemanager(); try { pm.getpackageinfo(packagename, 0); } catch (namenotfoundexception e) { return false; } return true; } /** * 拨打电话 * * @param context * @param phonenum */ public void call(context context, string phonenum) throws exception { if (phonenum != null && !phonenum.equals("")) { uri uri = uri.parse("tel:" + phonenum); intent intent = new intent(intent.action_dial, uri); context.startactivity(intent); } } /** * 跳转到联系人界面 */ public void tochoosecontactslist(context context) { intent intent = new intent(); intent.setaction(intent.action_view); intent.setdata(contacts.people.content_uri); context.startactivity(intent); } /** * 发送短信界面 ,现在好像不行了 */ public void tosendmessageactivity(context context, string number) { try { uri uri = uri.parse("smsto:" + number); intent intent = new intent(intent.action_sendto, uri); context.startactivity(intent); } catch (exception e) { e.printstacktrace(); } } /** * 打开网页 */ public void openweb(context context, string url) { try { uri uri = uri.parse(url); context.startactivity(new intent(intent.action_view, uri)); } catch (exception e) { e.printstacktrace(); } } /** * 获取应用权限 名称列表 */ public string[] getapppermissions(context context) throws namenotfoundexception { packagemanager pm = context.getpackagemanager(); packageinfo packageinfo = pm.getpackageinfo(context.getpackagename(), packagemanager.get_permissions); return getapppermissions(packageinfo); } public string[] getapppermissions(packageinfo packageinfo) throws namenotfoundexception { return packageinfo.requestedpermissions; } /** * 获取手机内安装的应用 */ public list<packageinfo> getinstalledapp(context context) { packagemanager pm = context.getpackagemanager(); return pm.getinstalledpackages(0); } /** * 获取手机安装非系统应用 */ @suppresswarnings("static-access") public list<packageinfo> getuserinstalledapp(context context) { list<packageinfo> infos = getinstalledapp(context); list<packageinfo> apps = new arraylist<packageinfo>(); for (packageinfo info : infos) { if ((info.applicationinfo.flags & info.applicationinfo.flag_system) <= 0) { apps.add(info); } } infos.clear(); infos = null; return apps; } /** * 获取安装应用的信息 */ public map<string, object> getinstalledappinfo(context context, packageinfo info) { map<string, object> appinfos = new hashmap<string, object>(); packagemanager pm = context.getpackagemanager(); applicationinfo aif = info.applicationinfo; appinfos.put("icon", pm.getapplicationicon(aif)); appinfos.put("lable", pm.getapplicationlabel(aif)); appinfos.put("packagename", aif.packagename); return appinfos; } /** * 打开指定包名的应用 */ public void startapppkg(context context, string pkg) { intent startintent = context.getpackagemanager() .getlaunchintentforpackage(pkg); startintent.setflags(intent.flag_activity_new_task); context.startactivity(startintent); } /** * 卸载指定包名的应用 */ public void uninstallapk(context context, string packagename) { uri uri = uri.parse("package:" + packagename); intent intent = new intent(intent.action_delete); intent.setdata(uri); context.startactivity(intent); } /** * 手机号判断 */ public static boolean ismobileno(string mobile) { pattern p = pattern .compile("^((145|147)|(15[^4])|(17[0-9])|((13|18)[0-9]))\\d{8}$"); matcher m = p.matcher(mobile); return m.matches(); } }
activity的调用示例代码:
package com.example.phone; import android.app.activity; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.os.bundle; import android.view.view; import android.widget.toast; import java.util.arrays; import java.util.list; import java.util.map; /** * 手机管理工具类的使用示例 */ public class myactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } /** * 获取手机系统版本号 * * @return */ public void getsdkversionnumber(view view) { integer sdkversionnumber = appphonemgr.getinstance().getsdkversionnumber(); showtoast(" \n 获取手机系统版本号:" + sdkversionnumber); } /** * 获取手机型号 */ public void getphonemodel(view view) { string phonemodel = appphonemgr.getinstance().getphonemodel(); showtoast(" \n 获取手机型号:" + phonemodel); } /** * 获取手机宽度 */ public void getphonewidth(view view) { int phonewidth = appphonemgr.getinstance().getphonewidth(this); showtoast(" \n 获取手机宽度:" + phonewidth); } /** * 获取手机高度 */ public void getphoneheight(view view) { int phoneheight = appphonemgr.getinstance().getphoneheight(this); showtoast(" \n 获取手机宽度:" + phoneheight); } /** * 获取手机imei串号 ,gsm手机的 imei 和 cdma手机的 meid. * 需要权限:android.permission.read_phone_state.否则会报错 */ public void getphoneimei(view view) { string phoneimei = appphonemgr.getinstance().getphoneimei(this); showtoast(" \n 获取手机imei串号:" + phoneimei); } /** * 获取手机sim卡号 */ public void getphonesim(view view) { string phonesim = appphonemgr.getinstance().getphonesim(this); showtoast(" \n 获取手机sim卡号:" + phonesim); } /** * 获取手机号,不行?? */ public void getphonenum(view view) { string phonesim = appphonemgr.getinstance().getphonenum(this); showtoast(" \n 获取手机号:" + phonesim); } /** * 判断sd卡是否挂载 */ public void issdcardmount(view view) { boolean issdcardmount = appphonemgr.getinstance().issdcardmount(); showtoast(" \n 判断sd卡挂载:" + issdcardmount); } /** * 获取sd卡剩余空间的大小 */ public void getsdfreesize(view view) { long getsdfreesize = appphonemgr.getinstance().getsdfreesize(); showtoast(" \n 获取sd卡剩余空间的大小:" + getsdfreesize); } /** * 获取sd卡空间的总大小 */ public void getsdallsize(view view) { long getsdallsize = appphonemgr.getinstance().getsdallsize(); showtoast(" \n 获取sd卡剩余空间的大小:" + getsdallsize); } /** * 判断是否是平板 */ public void istablet(view view) { boolean istablet = appphonemgr.getinstance().istablet(this); showtoast(" \n 判断是否是平板:" + istablet); } /** * 判断一个apk是否安装 */ public void isapkinstalled(view view) { boolean isapkinstalled = appphonemgr.getinstance().isapkinstalled(this, "com.wanputech.abp"); showtoast(" \n 判断一个apk是否安装:" + isapkinstalled); } /** * 拨打电话 */ public void call(view view) { string phone = "13812345678"; try { appphonemgr.getinstance().call(this, phone); } catch (exception e) { showtoast(" \n 拨打电话失败:" + e.getmessage()); } showtoast(" \n 拨打电话:" + phone); } /** * 跳转到联系人界面 */ public void tochoosecontactslist(view view) { appphonemgr.getinstance().tochoosecontactslist(this); } /** * 发送短信界面 */ public void tosendmessageactivity(view view) { appphonemgr.getinstance().tosendmessageactivity(this, "12315"); } /** * 打开网页 */ public void openweb(view view) { string url = "https://www.baidu.com/"; //百度 appphonemgr.getinstance().openweb(this, url); } /** * 获取应用权限 名称列表 */ public void getapppermissions(view view) { try { string[] getapppermissions = appphonemgr.getinstance().getapppermissions(this); showtoast(" \n 获取应用权限 名称列表:" + getapppermissions); } catch (exception e) { showtoast(" \n 获取应用权限 名称列表:" + e.getmessage()); } } /** * 程序请求了的权限 */ public void getapppermissions2(view view) { try { string[] getapppermissions = appphonemgr.getinstance().getapppermissions(getpackagemanager().getpackageinfo("com.example.phone", 1)); showtoast(" \n 程序请求了的权限:" + arrays.aslist(getapppermissions)); } catch (exception e) { showtoast(" \n 程序请求了的权限:" + e.getmessage()); } } /** * 获取手机内安装的应用 */ public void getinstalledapp(view view) { list<packageinfo> installedapp = appphonemgr.getinstance().getinstalledapp(this); for (int i = 0; i < installedapp.size(); i++) { showtoast(" \n 获取手机内安装的应用:" + installedapp.get(i)); } } /** * 获取手机安装非系统应用 */ public void getuserinstalledapp(view view) { list<packageinfo> installedapp = appphonemgr.getinstance().getuserinstalledapp(this); for (int i = 0; i < installedapp.size(); i++) { showtoast(" \n 获取手机安装非系统应用:" + installedapp.get(i)); } } /** * 获取安装应用的信息 */ public void getinstalledappinfo(view view) { try { map<string, object> installedappinfo = appphonemgr.getinstance().getinstalledappinfo(this, getpackagemanager().getpackageinfo("com.example.phone", 1)); for (int i = 0; i < installedappinfo.size(); i++) { showtoast(" \n 获取安装应用的信息:" + installedappinfo.get(i)); } } catch (packagemanager.namenotfoundexception e) { showtoast(" \n 获取安装应用的信息:" + e.getmessage()); } } /** * 打开指定包名的应用 */ public void startapppkg(view view) { try { //我另一个程序的包名com.wanputech.abp appphonemgr.getinstance().startapppkg(this, "com.wanputech.abp"); } catch (exception e) { showtoast("错误:" + e.getmessage()); } } /** * 卸载指定包名的应用 */ public void uninstallapk(view view) { try { //我另一个程序的包名com.example.column string packagename = "com.example.column"; appphonemgr.getinstance().uninstallapk(this, packagename); } catch (exception e) { showtoast("错误:" + e.getmessage()); } } /** * 手机号判断 */ public void ismobileno(view view) { // string phonenum = "12345678978"; boolean mobileno = appphonemgr.ismobileno(phonenum); showtoast(" \n 手机号判断:" + phonenum + "--" + mobileno); } toast toast; public void showtoast(string msg) { if (toast == null) { toast = toast.maketext(this, msg, toast.length_short); } else { toast.settext(msg); } toast.show(); } }
效果就不展示了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android 后台发送邮件到指定邮箱