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 ListgetInstalledApp(Context context) { PackageManager pm = context.getPackageManager(); return pm.getInstalledPackages(0); } /** * 获取手机安装非系统应用 */ @SuppressWarnings("static-access") public List getUserInstalledApp(Context context) { List infos = getInstalledApp(context); List apps = new ArrayList (); for (PackageInfo info : infos) { if ((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) { apps.add(info); } } infos.clear(); infos = null; return apps; } /** * 获取安装应用的信息 */ public Map getInstalledAppInfo(Context context, PackageInfo info) { Map appInfos = new HashMap (); 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) { ListinstalledApp = AppPhoneMgr.getInstance().getInstalledApp(this); for (int i = 0; i < installedApp.size(); i++) { showToast(" \n 获取手机内安装的应用:" + installedApp.get(i)); } } /** * 获取手机安装非系统应用 */ public void getUserInstalledApp(View view) { List 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 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(); } }
效果就不展示了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。