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手机管理工具类详解全部内容,希望文章能够帮你解决Android手机管理工具类详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)