在开发过程中,总会遇到很多其实经常写,但又死活记不住(也许是本人比较笨),每次都需要google的一些代码。这篇文章就是记录那些代码,以方便以后快速找到。
跳转到系统图库页面
1 | Intent albumIntent = new Intent(Intent.ACTION_PICK); |
跳转到系统相机页面
1 | Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); |
判断程序是否在后台
1 | ActivityManager activityManager = (ActivityManager) context.getApplicationContext() |
发送/取消闹钟
1 | Intent intent = new Intent(context, AlarmClockBroadcast.class); |
屏幕宽度高度
1 | DisplayMetrics dm = context.getResources().getDisplayMetrics(); |
Android获取StatusBar高度
1 | int x = 0, statusBarHeight = 0; |
获取设备的imsi
1 | TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
获取设备的imei
1 | TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
获取应用版本和版本号
1 | String appVersion=context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; |
获取屏幕亮度
1 | ContentResolver resolver = activity.getContentResolver(); |
dp转px
1 | DisplayMetrics dm = context.getResources().getDisplayMetrics(); |
px转dp
1 | DisplayMetrics dm = context.getResources().getDisplayMetrics(); |
sp转px
1 | final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; |
px转sp
1 | final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; |
获取缓存文件
1 | File f =context.getExternalCacheDir(); |
获取SD卡路径
1 | File f =context.getExternalCacheDir(); |
获取根目录文件
1 | File f = context.getExternalFilesDir(""); |
写文件
1 | File file = new File(fileName); |
读文件
1 | FileInputStream fin = new FileInputStream(fileName); |
读取assets或资源文件
1 | //InputStream is = activity.getResources().openRawResource(id) |
Bitmap保存文件
1 | FileOutputStream fos = new FileOutputStream(file, false); |
Bitmap.Option的设置
1 | BitmapFactory.Options options = new BitmapFactory.Options(); |
Bitmap旋转角度
1 | ExifInterface exif = null; |
MD5加密
1 | char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
监听锁屏开屏
1 | IntentFilter filter = new IntentFilter(); |
直接判断屏幕是否关闭
1 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
设置StatusBar颜色
1 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
设置StatusBar透明
1 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
设置MIUI状态栏主题
1 | boolean result = false; |
设置Flyme状态栏主题
1 | boolean result = false; |
时间Long类型转固定格式
1 | SimpleDateFormat simpleDateFormat=new SimpleDateFormat(style, Locale.getDefault()); |