首页 文章 精选 留言 我的

精选列表

搜索[项目实战],共10013篇文章
优秀的个人博客,低调大师

项目实战工具类(一):PhoneUtil(手机信息相关)

可以使用的功能: 1、获取手机系统版本号 2、获取手机型号 3、获取手机宽度 4、获取手机高度 5、获取手机imei串号 ,GSM手机的 IMEI 和 CDMA手机的 MEID. 6、获取手机sim卡号 7、获取手机号 8、判断sd卡是否挂载 9、获取sd卡剩余空间的大小 10、获取sd卡空间的总大小 11、判断是否是平板 12、判断一个apk是否安装 13、拨打电话 14、打开网页 15、获取应用权限 名称列表 16、获取手机内安装的应用 17、获取手机安装非系统应用 18、获取安装应用的信息 19、打开指定包名的应用 20、卸载指定包名的应用 21、手机号判断 工具类: 1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 import android.content.Context; 10 import android.content.Intent; 11 import android.content.pm.ApplicationInfo; 12 import android.content.pm.PackageInfo; 13 import android.content.pm.PackageManager; 14 import android.content.pm.PackageManager.NameNotFoundException; 15 import android.content.pm.PermissionGroupInfo; 16 import android.content.pm.PermissionInfo; 17 import android.content.pm.ResolveInfo; 18 import android.content.res.Configuration; 19 import android.net.Uri; 20 import android.os.Environment; 21 import android.os.StatFs; 22 import android.telephony.TelephonyManager; 23 import android.util.TypedValue; 24 import android.view.WindowManager; 25 26 public class PhoneUtil { 27 private static PhoneUtil phoneUtil; 28 29 public static PhoneUtil getInstance() { 30 if (phoneUtil == null) { 31 synchronized (PhoneUtil.class) { 32 if (phoneUtil == null) { 33 phoneUtil = new PhoneUtil(); 34 } 35 } 36 } 37 return phoneUtil; 38 } 39 40 /** 41 * 获取手机系统版本号 42 * 43 * @return 44 */ 45 public int getSDKVersionNumber() { 46 int sdkVersion; 47 try { 48 sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT); 49 } catch (NumberFormatException e) { 50 e.printStackTrace(); 51 sdkVersion = 0; 52 } 53 return sdkVersion; 54 } 55 56 /** 57 * 获取手机型号 58 */ 59 public String getPhoneModel() { 60 return android.os.Build.MODEL; 61 } 62 63 /** 64 * 获取手机宽度 65 */ 66 @SuppressWarnings("deprecation") 67 public int getPhoneWidth(Context context) { 68 WindowManager wm = (WindowManager) context 69 .getSystemService(Context.WINDOW_SERVICE); 70 return wm.getDefaultDisplay().getWidth(); 71 } 72 73 /** 74 * 获取手机高度 75 * 76 * @param context 77 */ 78 @SuppressWarnings("deprecation") 79 public int getPhoneHeight(Context context) { 80 WindowManager wm = (WindowManager) context 81 .getSystemService(Context.WINDOW_SERVICE); 82 return wm.getDefaultDisplay().getHeight(); 83 } 84 85 /** 86 * 获取手机imei串号 ,GSM手机的 IMEI 和 CDMA手机的 MEID. 87 * 88 * @param context 89 */ 90 public String getPhoneImei(Context context) { 91 TelephonyManager tm = (TelephonyManager) context 92 .getSystemService(Context.TELEPHONY_SERVICE); 93 if (tm == null) 94 return null; 95 return tm.getDeviceId(); 96 } 97 98 /** 99 * 获取手机sim卡号 100 * 101 * @param context 102 */ 103 public String getPhoneSim(Context context) { 104 TelephonyManager tm = (TelephonyManager) context 105 .getSystemService(Context.TELEPHONY_SERVICE); 106 if (tm == null) 107 return null; 108 return tm.getSubscriberId(); 109 } 110 111 /** 112 * 获取手机号 113 * 114 * @param context 115 */ 116 public String getPhoneNum(Context context) { 117 TelephonyManager tm = (TelephonyManager) context 118 .getSystemService(Context.TELEPHONY_SERVICE); 119 if (tm == null) 120 return null; 121 return tm.getLine1Number(); 122 } 123 124 /** 125 * 判断sd卡是否挂载 126 */ 127 public boolean isSDCardMount() { 128 if (android.os.Environment.getExternalStorageState().equals( 129 android.os.Environment.MEDIA_MOUNTED)) { 130 return true; 131 } else { 132 return false; 133 } 134 } 135 136 /** 137 * 获取sd卡剩余空间的大小 138 */ 139 @SuppressWarnings("deprecation") 140 public long getSDFreeSize() { 141 File path = Environment.getExternalStorageDirectory(); // 取得SD卡文件路径 142 StatFs sf = new StatFs(path.getPath()); 143 long blockSize = sf.getBlockSize(); // 获取单个数据块的大小(Byte) 144 long freeBlocks = sf.getAvailableBlocks();// 空闲的数据块的数量 145 // 返回SD卡空闲大小 146 return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB 147 } 148 149 /** 150 * 获取sd卡空间的总大小 151 */ 152 @SuppressWarnings("deprecation") 153 public long getSDAllSize() { 154 File path = Environment.getExternalStorageDirectory(); // 取得SD卡文件路径 155 StatFs sf = new StatFs(path.getPath()); 156 long blockSize = sf.getBlockSize(); // 获取单个数据块的大小(Byte) 157 long allBlocks = sf.getBlockCount(); // 获取所有数据块数 158 // 返回SD卡大小 159 return (allBlocks * blockSize) / 1024 / 1024; // 单位MB 160 } 161 162 /** 163 * 判断是否是平板 164 */ 165 public boolean isTablet(Context context) { 166 return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 167 } 168 169 /** 170 * 判断一个apk是否安装 171 * 172 * @param context 173 * @param packageName 174 */ 175 public boolean isApkInstalled(Context context, String packageName) { 176 PackageManager pm = context.getPackageManager(); 177 try { 178 pm.getPackageInfo(packageName, 0); 179 } catch (PackageManager.NameNotFoundException e) { 180 return false; 181 } 182 return true; 183 } 184 185 /** 186 * 拨打电话 187 * 188 * @param context 189 * @param phoneNum 190 */ 191 public void call(Context context, String phoneNum) throws Exception { 192 if (phoneNum != null && !phoneNum.equals("")) { 193 Uri uri = Uri.parse("tel:" + phoneNum); 194 Intent intent = new Intent(Intent.ACTION_DIAL, uri); 195 context.startActivity(intent); 196 } 197 } 198 199 /** 200 * 打开网页 201 */ 202 public void openWeb(Context context, String url) { 203 try { 204 Uri uri = Uri.parse(url); 205 context.startActivity(new Intent(Intent.ACTION_VIEW, uri)); 206 } catch (Exception e) { 207 e.printStackTrace(); 208 } 209 } 210 211 /** 212 * 获取应用权限 名称列表 213 */ 214 public String[] getAppPermissions(Context context) 215 throws NameNotFoundException { 216 PackageManager pm = context.getPackageManager(); 217 PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 218 PackageManager.GET_PERMISSIONS); 219 return getAppPermissions(packageInfo); 220 } 221 222 public String[] getAppPermissions(PackageInfo packageInfo) 223 throws NameNotFoundException { 224 return packageInfo.requestedPermissions; 225 } 226 227 /** 228 * 获取手机内安装的应用 229 */ 230 public List<PackageInfo> getInstalledApp(Context context) { 231 PackageManager pm = context.getPackageManager(); 232 return pm.getInstalledPackages(0); 233 } 234 235 /** 236 * 获取手机安装非系统应用 237 */ 238 @SuppressWarnings("static-access") 239 public List<PackageInfo> getUserInstalledApp(Context context) { 240 List<PackageInfo> infos = getInstalledApp(context); 241 List<PackageInfo> apps = new ArrayList<PackageInfo>(); 242 for (PackageInfo info : infos) { 243 if ((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) { 244 apps.add(info); 245 } 246 } 247 infos.clear(); 248 infos = null; 249 return apps; 250 } 251 252 /** 253 * 获取安装应用的信息 254 */ 255 public Map<String, Object> getInstalledAppInfo(Context context, 256 PackageInfo info) { 257 Map<String, Object> appInfos = new HashMap<String, Object>(); 258 PackageManager pm = context.getPackageManager(); 259 ApplicationInfo aif = info.applicationInfo; 260 appInfos.put("icon", pm.getApplicationIcon(aif)); 261 appInfos.put("lable", pm.getApplicationLabel(aif)); 262 appInfos.put("packageName", aif.packageName); 263 return appInfos; 264 } 265 266 /** 267 * 打开指定包名的应用 268 */ 269 public void startAppPkg(Context context, String pkg) { 270 Intent startIntent = context.getPackageManager() 271 .getLaunchIntentForPackage(pkg); 272 startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 273 context.startActivity(startIntent); 274 } 275 276 /** 277 * 卸载指定包名的应用 278 */ 279 public void unInstallApk(Context context, String packageName) { 280 Uri uri = Uri.parse("package:" + packageName); 281 Intent intent = new Intent(Intent.ACTION_DELETE); 282 intent.setData(uri); 283 context.startActivity(intent); 284 } 285 286 /** 287 * 手机号判断 288 */ 289 public static boolean isMobileNO(String mobile) { 290 Pattern p = Pattern 291 .compile("^((145|147)|(15[^4])|(17[0-9])|((13|18)[0-9]))\\d{8}$"); 292 Matcher m = p.matcher(mobile); 293 return m.matches(); 294 } 295 296 }

优秀的个人博客,低调大师

2.3.3 CGLIB动态代理 -《SSM深入解析与项目实战

2.3.3 CGLIB动态代理 前面介绍了JDK的动态代理,从一些实例也可以看到,JDK动态代理是依赖于实现的接口的。而CGLIB弥补了这个缺点,让我们在不需要实现接口的情况下,也可以实现动态代理。JDK动态代理和CGLIB动态代理在Spring实现AOP中都是使用到的技术,Spring AOP默认是使用JDK动态代理来代理接口的,但是可以进行强制使用CGLIB动态代理。 CGLIB内部使用了ASM(Java字节码操控框架)来进行转换字节码。可以代理没有接口类的类。所以相比较JDK动态代理来说,灵活一些,更值得称赞的地方是,由于CGLIB第通过字节码产生子类进行覆盖委托类的非final方法进行代理,而JDK动态代理使用Java类反射进行代理,所以CGLIB动态代理比JDK动态代理更快。注意,CGLIB不能对final和私有方法进行代理。 实例演示。首先创建一个委托类(目标类)。 代码清单2-25:AliPay目标类 public class AliPay { public void pay(String operation) { System.out.println("进行AliPay支付,操作:" + operation); } } 可以看到,在这里我们不再实现Pay接口了。 接下来我们需要引入一个依赖。在pom.xml文件中增加如下代码。不使用该包下的,使用org.springframework.cglib.proxy下的MethodInterceptor 类也是可以的。 代码清单2-26:pom.xml文件增加如下代码 <dependencies> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> </dependencies> 接下来就是直接写CGLIB的代理类。 更多内容请阅读原文:https://chenhx.blog.csdn.net/article/details/108027551

优秀的个人博客,低调大师

2.3.1 理解动态代理 -《SSM深入解析与项目实战

2.3 动态代理 Spring中AOP的拦截功能就是使用Java中的动态代理实现的。也就是在被代理类(方法)的基础上增加切面逻辑,生成代理类(方法)。切面的逻辑可以在目标类函数执行之前或者执行之后,或者在目标函数抛出异常的时候执行,则需要重写不同的方法。在本节中对于Spring中的AOP不进行过多的讲解。接下来详细讲解Java中的动态代理。 2.3.1 理解动态代理 为了更方便读者理解,在介绍之前,先介绍一下几个本文关于代理的名词。 委托类 - 被代理的类(也可以叫目标类) 代理类 - 进行代理的类 消费类 - 调用代理类的类 在理解动态代理之前,我想给大家介绍一下静态代理。很容易理解,静态,也就是代理类在程序运行之前就已经确定的,那么对该类进行代理的方式,即可称为静态代理。 一般情况下,静态代理中的代理类和委托类都会继承相同的父类或者实现相同的接口。 更多内容请查看原文地址: https://chenhx.blog.csdn.net/article/details/107964007

优秀的个人博客,低调大师

Android项目实战(四十):Andoird 7.0+ 安装APK适配

首先看一下安装apk文件的代码 /** * 通过隐式意图调用系统安装程序安装APK */ public static void install(Context context) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile( new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "xxxx.apk")), "application/vnd.android.package-archive"); context.startActivity(intent); } 测试发现该段代码在7.0一下的机型上可以成功打开指定路径下的指定apk文件 , 但是在7.0+的机型上调用该代码会报错: android.os.FileUriExposedException: file:///storage/emulated/0/Download/xxxx.apk exposed beyond app through Intent.getData() 原因在于:Android 7.0 版本开始 禁止向你的应用外公开 file:// URI。 如果一项包含文件 file:// URI类型 的 Intent 离开你的应用,应用失败,并出现 FileUriExposedException 异常。 解决方法: 一、在AndroidManifest.xml 文件中添加 四大组件之一的 <provider> <!-- 适配7.0 apk安装 --> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.xxx.xxxx.fileprovider" android:grantUriPermissions="true" android:exported="false"> <!--元数据--> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> 注意这里的 android :authorities 属性的值 中的com.xxx.xxxx 是你的包名,不可随意填写 二、res 目录下 建一个xml 文件,并新建xml文件file_paths.xml 注意文件名要和第一步中的 resource 属性的值一致 内容为: <?xml version="1.0" encoding="utf-8"?> <paths> <external-path path="." name="download"/> </paths> 三、根据机型的Android系统级别执行不同的安装apk的代码 注意,根据系统版本执行不同代码,7.0以下调用7.0+的代码会报错,7.0+的调用7.0以下的会报错。 if (file!=null){ // file 即 apk文件 Intent intent = new Intent(Intent.ACTION_VIEW); // 由于没有在Activity环境下启动Activity,设置下面的标签 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(Build.VERSION.SDK_INT>=24) { //判读版本是否在7.0以上 //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件 Uri apkUri = FileProvider.getUriForFile(context, "com.xxx.xxxxx.fileprovider", file); //添加这一句表示对目标应用临时授权该Uri所代表的文件 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); }else{ intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } context.startActivity(intent); }

优秀的个人博客,低调大师

Android项目实战(三十六):给背景加上阴影效果

圆角背景大家应该经常用: 一个drawable资源文件 里面控制corner圆角 和solid填充色 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="@dimen/dp_2"></corners> <solid android:color="@color/standard_main"></solid> </shape> 那么在此基础上 , 实现带阴影效果的圆角背景 代码如下 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 阴影部分 --> <!-- 个人觉得更形象的表达:top代表下边的阴影高度,left代表右边的阴影宽度。其实也就是相对应的offset,solid中的颜色是阴影的颜色,也可以设置角度等等 --> <item android:left="4dp" android:top="4dp"> <shape android:shape="rectangle" > <gradient android:angle="270" android:endColor="#0F000000" android:startColor="#0F000000" /> <corners android:bottomLeftRadius="@dimen/dp_4" android:bottomRightRadius="@dimen/dp_14" android:topLeftRadius="@dimen/dp_4" android:topRightRadius="@dimen/dp_4" /> </shape> </item> <!-- 背景部分 --> <!-- 形象的表达:bottom代表背景部分在上边缘超出阴影的高度,right代表背景部分在左边超出阴影的宽度(相对应的offset) --> <item android:bottom="3dp" android:left="@dimen/dp_0.5" android:top="@dimen/dp_0.5" android:right="3dp"> <shape android:shape="rectangle" > <gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#FFFFFF" /> <corners android:bottomLeftRadius="@dimen/dp_4" android:bottomRightRadius="@dimen/dp_14" android:topLeftRadius="@dimen/dp_4" android:topRightRadius="@dimen/dp_4" /> </shape> </item> </layer-list> 效果: 可以看到 右侧和下侧都有一个小范围的灰色阴影效果。 在实际产品中作为列表item的背景效果: 是不是实现了一种类似cardview的效果

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册