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 getInstalledApp(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<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 {
gr.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) [外链图片转存中…(img-9A8XPBmp-1643608173590)]
{
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 {