新手必备的常用代码片段整理(一)
2016-07-01 16:48
295 查看
以下内容来自多个开源项目的整理和自己的项目积累
拨打电话
跳转至拨号界面
发送短信
唤醒屏幕并解锁
判断当前App处于前台还是后台状态
判断当前手机是否处于锁屏睡眠状态
判断当前是否有网络连接
判断当前是否是WIFI连接状态
安装APK
判断当前设备是否为手机
获取当前设备宽高单位px
获取当前设备的IMEI需要与上面的isPhone一起使用
获取当前设备的MAC地址
获取当前程序的版本号
uses-permission android:name=”android.permission.WAKE_LOCK”
uses-permission android:name=”android.permission.DISABLE_KEYGUARD”
uses-permission android:name=”android.permission.GET_TASKS”
拨打电话
跳转至拨号界面
发送短信
唤醒屏幕并解锁
判断当前App处于前台还是后台状态
判断当前手机是否处于锁屏睡眠状态
判断当前是否有网络连接
判断当前是否是WIFI连接状态
安装APK
判断当前设备是否为手机
获取当前设备宽高单位px
获取当前设备的IMEI需要与上面的isPhone一起使用
获取当前设备的MAC地址
获取当前程序的版本号
拨打电话
public static void call(Context context, String phoneNumber) { context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber))); }
跳转至拨号界面
public static void callDial(Context context, String phoneNumber) { context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber))); }
发送短信
public static void sendSms(Context context, String phoneNumber, String content) { Uri uri = Uri.parse("smsto:" + (TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber)); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", TextUtils.isEmpty(content) ? "" : content); context.startActivity(intent); }
唤醒屏幕并解锁
需要添加权限:uses-permission android:name=”android.permission.WAKE_LOCK”
uses-permission android:name=”android.permission.DISABLE_KEYGUARD”
public static void wakeUpAndUnlock(Context context){ KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock"); //解锁 kl.disableKeyguard(); //获取电源管理器对象 PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE); //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright"); //点亮屏幕 wl.acquire(); //释放 wl.release(); }
判断当前App处于前台还是后台状态
需要添加权限:uses-permission android:name=”android.permission.GET_TASKS”
public static boolean isApplicationBackground(final Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); @SuppressWarnings("deprecation") List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
判断当前手机是否处于锁屏睡眠状态
public static boolean isSleeping(Context context) { KeyguardManager kgMgr = (KeyguardManager) context .getSystemService(Context.KEYGUARD_SERVICE); boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode(); return isSleeping; }
判断当前是否有网络连接
public static boolean isOnline(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Activity.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.isConnected()) { return true; } return false; }
判断当前是否是WIFI连接状态
public static boolean isWifiConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetworkInfo.isConnected()) { return true; } return false; }
安装APK
public static void installApk(Context context, File file) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setType("application/vnd.android.package-archive"); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); }
判断当前设备是否为手机
public static boolean isPhone(Context context) { TelephonyManager telephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) { return false; } else { return true; } }
获取当前设备宽高单位px
@SuppressWarnings("deprecation") public static int getDeviceWidth(Context context) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); return manager.getDefaultDisplay().getWidth(); } @SuppressWarnings("deprecation") public static int getDeviceHeight(Context context) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); return manager.getDefaultDisplay().getHeight(); }
获取当前设备的IMEI需要与上面的isPhone一起使用
@TargetApi(Build.VERSION_CODES.CUPCAKE) public static String getDeviceIMEI(Context context) { String deviceId; if (isPhone(context)) { TelephonyManager telephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); deviceId = telephony.getDeviceId(); } else { deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } return deviceId; }
获取当前设备的MAC地址
public static String getMacAddress(Context context) { String macAddress; WifiManager wifi = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); macAddress = info.getMacAddress(); if (null == macAddress) { return ""; } macAddress = macAddress.replace(":", ""); return macAddress; }
获取当前程序的版本号
public static String getAppVersion(Context context) { String version = "0"; try { version = context.getPackageManager().getPackageInfo( context.getPackageName(), 0).versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return version; }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android wifi 无线调试
- 基于 Linux 的智能手机 Librem 5 开启预售
- 5 个可以满足你的生产力、沟通和娱乐需求的开源手机应用
- 每日安全资讯:哪些属于App违法违规收集使用个人信息?
- 每日安全资讯:命案侦破过程揭示 Google 能够跟踪全世界的手机
- 虚拟化基础架构Windows 2008篇之10-使用WDS安装Windows 7
- 拨号网络的简单知识
- 菜鸟必看网络名词
- 通晓网络测试常用命令
- 网络路由技术及运用2
- IP网络路由技术
- 网络防火墙的设置技巧
- 网络管理之IP地址篇
- 网络路由技术及运用3
- 网络路由技术及运用4
- 网络路由技术及运用1
- IP地址的分类及范围详解:A、B、C、D、E五类是如何划分的?