您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Android中怎么實(shí)現(xiàn)一個(gè)手機(jī)管理工具類,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
工具類內(nèi)的方法:
getInstance : 單例對(duì)象
getSDKVersionNumber : 獲取手機(jī)系統(tǒng)版本號(hào)
getPhoneModel : 獲取手機(jī)型號(hào)
getPhoneWidth : 獲取手機(jī)寬度
getPhoneHeight : 獲取手機(jī)高度
getPhoneImei : 獲取手機(jī)imei串號(hào) ,GSM手機(jī)的 IMEI 和 CDMA手機(jī)的 MEID
getPhoneSim : 獲取手機(jī)sim卡號(hào)
getPhoneNum : 獲取手機(jī)號(hào)
isSDCardMount : 判斷sd卡是否掛載
getSDFreeSize : 獲取sd卡剩余空間的大小
getSDAllSize : 獲取sd卡空間的總大小
isTablet : 判斷是否是平板
isApkInstalled : 判斷一個(gè)apk是否安裝
getAppPermissions : 獲取應(yīng)用權(quán)限 名稱列表
getInstalledApp : 獲取手機(jī)內(nèi)安裝的應(yīng)用
getUserInstalledApp : 獲取手機(jī)安裝非系統(tǒng)應(yīng)用
getInstalledAppInfo : 獲取安裝應(yīng)用的信息
startAppPkg : 打開指定包名的應(yīng)用
unInstallApk : 卸載指定包名的應(yīng)用
callPhone : 直接呼叫指定的號(hào)碼
toCallPhoneActivity : 跳轉(zhuǎn)至撥號(hào)界面
sendMessage : 直接調(diào)用短信API發(fā)送信息(設(shè)置監(jiān)聽發(fā)送和接收狀態(tài))
toSendMessageActivity: 跳轉(zhuǎn)至發(fā)送短信界面(自動(dòng)設(shè)置接收方的號(hào)碼)
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; /** * 主要功能:手機(jī)管理工具類 */ 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; } /** * 獲取手機(jī)系統(tǒng)版本號(hào) * * @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; } /** * 獲取手機(jī)型號(hào) */ public String getPhoneModel() { return android.os.Build.MODEL; } /** * 獲取手機(jī)寬度 */ @SuppressWarnings("deprecation") public int getPhoneWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); return wm.getDefaultDisplay().getWidth(); } /** * 獲取手機(jī)高度 * * @param context */ @SuppressWarnings("deprecation") public int getPhoneHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); return wm.getDefaultDisplay().getHeight(); } /** * 獲取手機(jī)imei串號(hào) ,GSM手機(jī)的 IMEI 和 CDMA手機(jī)的 MEID. * * @param context */ public String getPhoneImei(Context context) { TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) return null; return tm.getDeviceId(); } /** * 獲取手機(jī)sim卡號(hào) * * @param context */ public String getPhoneSim(Context context) { TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) return null; return tm.getSubscriberId(); } /** * 獲取手機(jī)號(hào) * * @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(); // 獲取單個(gè)數(shù)據(jù)塊的大小(Byte) long freeBlocks = sf.getAvailableBlocks();// 空閑的數(shù)據(jù)塊的數(shù)量 // 返回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(); // 獲取單個(gè)數(shù)據(jù)塊的大小(Byte) long allBlocks = sf.getBlockCount(); // 獲取所有數(shù)據(jù)塊數(shù) // 返回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; } /** * 判斷一個(gè)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); } } /** * 跳轉(zhuǎn)到聯(lián)系人界面 */ public void toChooseContactsList(Context context) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Contacts.People.CONTENT_URI); context.startActivity(intent); } /** * 發(fā)送短信界面 ,現(xiàn)在好像不行了 */ 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(); } } /** * 打開網(wǎng)頁 */ 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(); } } /** * 獲取應(yīng)用權(quán)限 名稱列表 */ 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; } /** * 獲取手機(jī)內(nèi)安裝的應(yīng)用 */ public List<PackageInfo> getInstalledApp(Context context) { PackageManager pm = context.getPackageManager(); return pm.getInstalledPackages(0); } /** * 獲取手機(jī)安裝非系統(tǒng)應(yīng)用 */ @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; } /** * 獲取安裝應(yīng)用的信息 */ 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; } /** * 打開指定包名的應(yīng)用 */ public void startAppPkg(Context context, String pkg) { Intent startIntent = context.getPackageManager() .getLaunchIntentForPackage(pkg); startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(startIntent); } /** * 卸載指定包名的應(yīng)用 */ 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); } /** * 手機(jī)號(hào)判斷 */ 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的調(diào)用示例代碼:
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; /** * 手機(jī)管理工具類的使用示例 */ public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } /** * 獲取手機(jī)系統(tǒng)版本號(hào) * * @return */ public void getSDKVersionNumber(View view) { Integer sdkVersionNumber = AppPhoneMgr.getInstance().getSDKVersionNumber(); showToast(" \n 獲取手機(jī)系統(tǒng)版本號(hào):" + sdkVersionNumber); } /** * 獲取手機(jī)型號(hào) */ public void getPhoneModel(View view) { String phoneModel = AppPhoneMgr.getInstance().getPhoneModel(); showToast(" \n 獲取手機(jī)型號(hào):" + phoneModel); } /** * 獲取手機(jī)寬度 */ public void getPhoneWidth(View view) { int phoneWidth = AppPhoneMgr.getInstance().getPhoneWidth(this); showToast(" \n 獲取手機(jī)寬度:" + phoneWidth); } /** * 獲取手機(jī)高度 */ public void getPhoneHeight(View view) { int phoneHeight = AppPhoneMgr.getInstance().getPhoneHeight(this); showToast(" \n 獲取手機(jī)寬度:" + phoneHeight); } /** * 獲取手機(jī)imei串號(hào) ,GSM手機(jī)的 IMEI 和 CDMA手機(jī)的 MEID. * 需要權(quán)限:android.permission.READ_PHONE_STATE.否則會(huì)報(bào)錯(cuò) */ public void getPhoneImei(View view) { String phoneImei = AppPhoneMgr.getInstance().getPhoneImei(this); showToast(" \n 獲取手機(jī)imei串號(hào):" + phoneImei); } /** * 獲取手機(jī)sim卡號(hào) */ public void getPhoneSim(View view) { String phoneSim = AppPhoneMgr.getInstance().getPhoneSim(this); showToast(" \n 獲取手機(jī)sim卡號(hào):" + phoneSim); } /** * 獲取手機(jī)號(hào),不行?? */ public void getPhoneNum(View view) { String phoneSim = AppPhoneMgr.getInstance().getPhoneNum(this); showToast(" \n 獲取手機(jī)號(hào):" + 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); } /** * 判斷一個(gè)apk是否安裝 */ public void isApkInstalled(View view) { boolean isApkInstalled = AppPhoneMgr.getInstance().isApkInstalled(this, "com.wanputech.abp"); showToast(" \n 判斷一個(gè)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); } /** * 跳轉(zhuǎn)到聯(lián)系人界面 */ public void toChooseContactsList(View view) { AppPhoneMgr.getInstance().toChooseContactsList(this); } /** * 發(fā)送短信界面 */ public void toSendMessageActivity(View view) { AppPhoneMgr.getInstance().toSendMessageActivity(this, "12315"); } /** * 打開網(wǎng)頁 */ public void openWeb(View view) { String url = "https://www.baidu.com/"; //百度 AppPhoneMgr.getInstance().openWeb(this, url); } /** * 獲取應(yīng)用權(quán)限 名稱列表 */ public void getAppPermissions(View view) { try { String[] getAppPermissions = AppPhoneMgr.getInstance().getAppPermissions(this); showToast(" \n 獲取應(yīng)用權(quán)限 名稱列表:" + getAppPermissions); } catch (Exception e) { showToast(" \n 獲取應(yīng)用權(quán)限 名稱列表:" + e.getMessage()); } } /** * 程序請(qǐng)求了的權(quán)限 */ public void getAppPermissions2(View view) { try { String[] getAppPermissions = AppPhoneMgr.getInstance().getAppPermissions(getPackageManager().getPackageInfo("com.example.phone", 1)); showToast(" \n 程序請(qǐng)求了的權(quán)限:" + Arrays.asList(getAppPermissions)); } catch (Exception e) { showToast(" \n 程序請(qǐng)求了的權(quán)限:" + e.getMessage()); } } /** * 獲取手機(jī)內(nèi)安裝的應(yīng)用 */ public void getInstalledApp(View view) { List<PackageInfo> installedApp = AppPhoneMgr.getInstance().getInstalledApp(this); for (int i = 0; i < installedApp.size(); i++) { showToast(" \n 獲取手機(jī)內(nèi)安裝的應(yīng)用:" + installedApp.get(i)); } } /** * 獲取手機(jī)安裝非系統(tǒng)應(yīng)用 */ public void getUserInstalledApp(View view) { List<PackageInfo> installedApp = AppPhoneMgr.getInstance().getUserInstalledApp(this); for (int i = 0; i < installedApp.size(); i++) { showToast(" \n 獲取手機(jī)安裝非系統(tǒng)應(yīng)用:" + installedApp.get(i)); } } /** * 獲取安裝應(yīng)用的信息 */ 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 獲取安裝應(yīng)用的信息:" + installedAppInfo.get(i)); } } catch (PackageManager.NameNotFoundException e) { showToast(" \n 獲取安裝應(yīng)用的信息:" + e.getMessage()); } } /** * 打開指定包名的應(yīng)用 */ public void startAppPkg(View view) { try { //我另一個(gè)程序的包名com.wanputech.abp AppPhoneMgr.getInstance().startAppPkg(this, "com.wanputech.abp"); } catch (Exception e) { showToast("錯(cuò)誤:" + e.getMessage()); } } /** * 卸載指定包名的應(yīng)用 */ public void unInstallApk(View view) { try { //我另一個(gè)程序的包名com.example.column String packageName = "com.example.column"; AppPhoneMgr.getInstance().unInstallApk(this, packageName); } catch (Exception e) { showToast("錯(cuò)誤:" + e.getMessage()); } } /** * 手機(jī)號(hào)判斷 */ public void isMobileNO(View view) { // String phoneNum = "12345678978"; boolean mobileNO = AppPhoneMgr.isMobileNO(phoneNum); showToast(" \n 手機(jī)號(hào)判斷:" + 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中怎么實(shí)現(xiàn)一個(gè)手機(jī)管理工具類,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。