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类的代码
packagecom.example.phone; importandroid.content.Context; importandroid.content.Intent; importandroid.content.pm.ApplicationInfo; importandroid.content.pm.PackageInfo; importandroid.content.pm.PackageManager; importandroid.content.pm.PackageManager.NameNotFoundException; importandroid.content.res.Configuration; importandroid.net.Uri; importandroid.os.Environment; importandroid.os.StatFs; importandroid.provider.Contacts; importandroid.telephony.TelephonyManager; importandroid.view.WindowManager; importjava.io.File; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importjava.util.regex.Matcher; importjava.util.regex.Pattern; /** *主要功能:手机管理工具类 */ publicclassAppPhoneMgr{ privatestaticAppPhoneMgrphoneUtil; publicstaticAppPhoneMgrgetInstance(){ if(phoneUtil==null){ synchronized(AppPhoneMgr.class){ if(phoneUtil==null){ phoneUtil=newAppPhoneMgr(); } } } returnphoneUtil; } /** *获取手机系统版本号 * *@return */ publicIntegergetSDKVersionNumber(){ IntegersdkVersion; try{ sdkVersion=Integer.valueOf(android.os.Build.VERSION.SDK_INT); }catch(NumberFormatExceptione){ e.printStackTrace(); sdkVersion=Integer.valueOf(0); } returnsdkVersion; } /** *获取手机型号 */ publicStringgetPhoneModel(){ returnandroid.os.Build.MODEL; } /** *获取手机宽度 */ @SuppressWarnings("deprecation") publicintgetPhoneWidth(Contextcontext){ WindowManagerwm=(WindowManager)context .getSystemService(Context.WINDOW_SERVICE); returnwm.getDefaultDisplay().getWidth(); } /** *获取手机高度 * *@paramcontext */ @SuppressWarnings("deprecation") publicintgetPhoneHeight(Contextcontext){ WindowManagerwm=(WindowManager)context .getSystemService(Context.WINDOW_SERVICE); returnwm.getDefaultDisplay().getHeight(); } /** *获取手机imei串号,GSM手机的IMEI和CDMA手机的MEID. * *@paramcontext */ publicStringgetPhoneImei(Contextcontext){ TelephonyManagertm=(TelephonyManager)context .getSystemService(Context.TELEPHONY_SERVICE); if(tm==null) returnnull; returntm.getDeviceId(); } /** *获取手机sim卡号 * *@paramcontext */ publicStringgetPhoneSim(Contextcontext){ TelephonyManagertm=(TelephonyManager)context .getSystemService(Context.TELEPHONY_SERVICE); if(tm==null) returnnull; returntm.getSubscriberId(); } /** *获取手机号 * *@paramcontext */ publicStringgetPhoneNum(Contextcontext){ TelephonyManagertm=(TelephonyManager)context .getSystemService(Context.TELEPHONY_SERVICE); if(tm==null) returnnull; returntm.getLine1Number(); } /** *判断sd卡是否挂载 */ publicbooleanisSDCardMount(){ if(Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)){ returntrue; }else{ returnfalse; } } /** *获取sd卡剩余空间的大小 */ @SuppressWarnings("deprecation") publiclonggetSDFreeSize(){ Filepath=Environment.getExternalStorageDirectory();//取得SD卡文件路径 StatFssf=newStatFs(path.getPath()); longblockSize=sf.getBlockSize();//获取单个数据块的大小(Byte) longfreeBlocks=sf.getAvailableBlocks();//空闲的数据块的数量 //返回SD卡空闲大小 return(freeBlocks*blockSize)/1024/1024;//单位MB } /** *获取sd卡空间的总大小 */ @SuppressWarnings("deprecation") publiclonggetSDAllSize(){ Filepath=Environment.getExternalStorageDirectory();//取得SD卡文件路径 StatFssf=newStatFs(path.getPath()); longblockSize=sf.getBlockSize();//获取单个数据块的大小(Byte) longallBlocks=sf.getBlockCount();//获取所有数据块数 //返回SD卡大小 return(allBlocks*blockSize)/1024/1024;//单位MB } /** *判断是否是平板 */ publicbooleanisTablet(Contextcontext){ return(context.getResources().getConfiguration().screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)>=Configuration.SCREENLAYOUT_SIZE_LARGE; } /** *判断一个apk是否安装 * *@paramcontext *@parampackageName */ publicbooleanisApkInstalled(Contextcontext,StringpackageName){ PackageManagerpm=context.getPackageManager(); try{ pm.getPackageInfo(packageName,0); }catch(NameNotFoundExceptione){ returnfalse; } returntrue; } /** *拨打电话 * *@paramcontext *@paramphoneNum */ publicvoidcall(Contextcontext,StringphoneNum)throwsException{ if(phoneNum!=null&&!phoneNum.equals("")){ Uriuri=Uri.parse("tel:"+phoneNum); Intentintent=newIntent(Intent.ACTION_DIAL,uri); context.startActivity(intent); } } /** *跳转到联系人界面 */ publicvoidtoChooseContactsList(Contextcontext){ Intentintent=newIntent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Contacts.People.CONTENT_URI); context.startActivity(intent); } /** *发送短信界面,现在好像不行了 */ publicvoidtoSendMessageActivity(Contextcontext,Stringnumber){ try{ Uriuri=Uri.parse("smsto:"+number); Intentintent=newIntent(Intent.ACTION_SENDTO,uri); context.startActivity(intent); }catch(Exceptione){ e.printStackTrace(); } } /** *打开网页 */ publicvoidopenWeb(Contextcontext,Stringurl){ try{ Uriuri=Uri.parse(url); context.startActivity(newIntent(Intent.ACTION_VIEW,uri)); }catch(Exceptione){ e.printStackTrace(); } } /** *获取应用权限名称列表 */ publicString[]getAppPermissions(Contextcontext) throwsNameNotFoundException{ PackageManagerpm=context.getPackageManager(); PackageInfopackageInfo=pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS); returngetAppPermissions(packageInfo); } publicString[]getAppPermissions(PackageInfopackageInfo) throwsNameNotFoundException{ returnpackageInfo.requestedPermissions; } /** *获取手机内安装的应用 */ publicListgetInstalledApp(Contextcontext){ PackageManagerpm=context.getPackageManager(); returnpm.getInstalledPackages(0); } /** *获取手机安装非系统应用 */ @SuppressWarnings("static-access") publicList getUserInstalledApp(Contextcontext){ List infos=getInstalledApp(context); List apps=newArrayList (); for(PackageInfoinfo:infos){ if((info.applicationInfo.flags&info.applicationInfo.FLAG_SYSTEM)<=0){ apps.add(info); } } infos.clear(); infos=null; returnapps; } /** *获取安装应用的信息 */ publicMap getInstalledAppInfo(Contextcontext, PackageInfoinfo){ Map appInfos=newHashMap (); PackageManagerpm=context.getPackageManager(); ApplicationInfoaif=info.applicationInfo; appInfos.put("icon",pm.getApplicationIcon(aif)); appInfos.put("lable",pm.getApplicationLabel(aif)); appInfos.put("packageName",aif.packageName); returnappInfos; } /** *打开指定包名的应用 */ publicvoidstartAppPkg(Contextcontext,Stringpkg){ IntentstartIntent=context.getPackageManager() .getLaunchIntentForPackage(pkg); startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(startIntent); } /** *卸载指定包名的应用 */ publicvoidunInstallApk(Contextcontext,StringpackageName){ Uriuri=Uri.parse("package:"+packageName); Intentintent=newIntent(Intent.ACTION_DELETE); intent.setData(uri); context.startActivity(intent); } /** *手机号判断 */ publicstaticbooleanisMobileNO(Stringmobile){ Patternp=Pattern .compile("^((145|147)|(15[^4])|(17[0-9])|((13|18)[0-9]))\\d{8}$"); Matcherm=p.matcher(mobile); returnm.matches(); } }
Activity的调用示例代码:
packagecom.example.phone; importandroid.app.Activity; importandroid.content.pm.PackageInfo; importandroid.content.pm.PackageManager; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Toast; importjava.util.Arrays; importjava.util.List; importjava.util.Map; /** *手机管理工具类的使用示例 */ publicclassMyActivityextendsActivity{ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); } /** *获取手机系统版本号 * *@return */ publicvoidgetSDKVersionNumber(Viewview){ IntegersdkVersionNumber=AppPhoneMgr.getInstance().getSDKVersionNumber(); showToast("\n获取手机系统版本号:"+sdkVersionNumber); } /** *获取手机型号 */ publicvoidgetPhoneModel(Viewview){ StringphoneModel=AppPhoneMgr.getInstance().getPhoneModel(); showToast("\n获取手机型号:"+phoneModel); } /** *获取手机宽度 */ publicvoidgetPhoneWidth(Viewview){ intphoneWidth=AppPhoneMgr.getInstance().getPhoneWidth(this); showToast("\n获取手机宽度:"+phoneWidth); } /** *获取手机高度 */ publicvoidgetPhoneHeight(Viewview){ intphoneHeight=AppPhoneMgr.getInstance().getPhoneHeight(this); showToast("\n获取手机宽度:"+phoneHeight); } /** *获取手机imei串号,GSM手机的IMEI和CDMA手机的MEID. *需要权限:android.permission.READ_PHONE_STATE.否则会报错 */ publicvoidgetPhoneImei(Viewview){ StringphoneImei=AppPhoneMgr.getInstance().getPhoneImei(this); showToast("\n获取手机imei串号:"+phoneImei); } /** *获取手机sim卡号 */ publicvoidgetPhoneSim(Viewview){ StringphoneSim=AppPhoneMgr.getInstance().getPhoneSim(this); showToast("\n获取手机sim卡号:"+phoneSim); } /** *获取手机号,不行?? */ publicvoidgetPhoneNum(Viewview){ StringphoneSim=AppPhoneMgr.getInstance().getPhoneNum(this); showToast("\n获取手机号:"+phoneSim); } /** *判断sd卡是否挂载 */ publicvoidisSDCardMount(Viewview){ booleanisSDCardMount=AppPhoneMgr.getInstance().isSDCardMount(); showToast("\n判断sd卡挂载:"+isSDCardMount); } /** *获取sd卡剩余空间的大小 */ publicvoidgetSDFreeSize(Viewview){ longgetSDFreeSize=AppPhoneMgr.getInstance().getSDFreeSize(); showToast("\n获取sd卡剩余空间的大小:"+getSDFreeSize); } /** *获取sd卡空间的总大小 */ publicvoidgetSDAllSize(Viewview){ longgetSDAllSize=AppPhoneMgr.getInstance().getSDAllSize(); showToast("\n获取sd卡剩余空间的大小:"+getSDAllSize); } /** *判断是否是平板 */ publicvoidisTablet(Viewview){ booleanisTablet=AppPhoneMgr.getInstance().isTablet(this); showToast("\n判断是否是平板:"+isTablet); } /** *判断一个apk是否安装 */ publicvoidisApkInstalled(Viewview){ booleanisApkInstalled=AppPhoneMgr.getInstance().isApkInstalled(this,"com.wanputech.abp"); showToast("\n判断一个apk是否安装:"+isApkInstalled); } /** *拨打电话 */ publicvoidcall(Viewview){ Stringphone="13812345678"; try{ AppPhoneMgr.getInstance().call(this,phone); }catch(Exceptione){ showToast("\n拨打电话失败:"+e.getMessage()); } showToast("\n拨打电话:"+phone); } /** *跳转到联系人界面 */ publicvoidtoChooseContactsList(Viewview){ AppPhoneMgr.getInstance().toChooseContactsList(this); } /** *发送短信界面 */ publicvoidtoSendMessageActivity(Viewview){ AppPhoneMgr.getInstance().toSendMessageActivity(this,"12315"); } /** *打开网页 */ publicvoidopenWeb(Viewview){ Stringurl="https://www.baidu.com/";//百度 AppPhoneMgr.getInstance().openWeb(this,url); } /** *获取应用权限名称列表 */ publicvoidgetAppPermissions(Viewview){ try{ String[]getAppPermissions=AppPhoneMgr.getInstance().getAppPermissions(this); showToast("\n获取应用权限名称列表:"+getAppPermissions); }catch(Exceptione){ showToast("\n获取应用权限名称列表:"+e.getMessage()); } } /** *程序请求了的权限 */ publicvoidgetAppPermissions2(Viewview){ try{ String[]getAppPermissions=AppPhoneMgr.getInstance().getAppPermissions(getPackageManager().getPackageInfo("com.example.phone",1)); showToast("\n程序请求了的权限:"+Arrays.asList(getAppPermissions)); }catch(Exceptione){ showToast("\n程序请求了的权限:"+e.getMessage()); } } /** *获取手机内安装的应用 */ publicvoidgetInstalledApp(Viewview){ ListinstalledApp=AppPhoneMgr.getInstance().getInstalledApp(this); for(inti=0;i installedApp=AppPhoneMgr.getInstance().getUserInstalledApp(this); for(inti=0;i installedAppInfo=AppPhoneMgr.getInstance().getInstalledAppInfo(this,getPackageManager().getPackageInfo("com.example.phone",1)); for(inti=0;i 效果就不展示了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。