Android系统工具类详解

2020-10-30 11:16:21 浏览数 (1)

本文实例为大家分享了Android系统工具类的具体代码,供大家参考,具体内容如下

系统工具类

代码语言:javascript复制
public class systemUtil {
//隐藏ipad底部虚拟按键栏
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void closeBottomBar(Activity activity){
Window _window = activity.getWindow();
WindowManager.LayoutParams params = _window.getAttributes();
params.systemUiVisibility =
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
_window.setAttributes(params);
}
//不自动弹出软键盘
public static void softInputMode(Activity activity){
activity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
//保持屏幕常亮
public static void screenLightUp(Activity activity){
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
//获取屏幕分辨率
public static int[] defaultDisplay(Activity activity){
int[] pixels = new int[2];
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
pixels[0]=dm.widthPixels;
pixels[1]=dm.heightPixels;
return pixels;
}
//获取Android系统版本
public static String getSystemVersion() {
return android.os.Build.VERSION.RELEASE;
}
//获取设备机型
public static String getSystemModel() {
return android.os.Build.MODEL;
}
//获取IMEI识别号
//所需权限 <uses-permission android:name="android.permission.READ_PHONE_STATE" / 
@SuppressLint("MissingPermission")
public static String getIMEI(Activity activity) {
//6.0以上的系统动态添加权限
if (ActivityCompat.checkSelfPermission(activity,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_PHONE_STATE},1);
}
TelephonyManager tm =
(TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);
return tm.getDeviceId();
}
//获取系统当前语言
public static String getSystemLanguage() {
return Locale.getDefault().getLanguage();
}
//获取设备电量
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static int getBattery(Context context){
BatteryManager batteryManager =
(BatteryManager)context.getSystemService(BATTERY_SERVICE);
return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
}
//获取设备内存可用大小(GB)
public static String getRomAvailableSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
//获取设备内存可用的总量大小(GB)
public static String getRomTotalSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
//获得SD卡可用总量大小
public static String getSDTotalSize(Context context) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
//获得sd卡可用大小
private String getSDAvailableSize(Context context) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
//重启设备
private void restartDevices() {
String cmd = "su -c reboot";
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
Log.i("restart","权限不足");
}
}
}

系统相关权限

代码语言:javascript复制
//写入外部存储
android.permission.WRITE_EXTERNAL_STORAGE,允许写入外部存储
//读取外部存储
android.permission.READ_EXTERNAL_STORAGE,允许读取外部存储
//读取系统日志
android.permission.READ_LOGS,读取系统底层日志
//读取短信内容
android.permission.READ_SMS,读取短信内容
//振动
android.permission.VIBRATE,允许振动
//重启设备
android.permission.REBOOT,允许程序重新启动设备
//安装应用程序
android.permission.INSTALL_PACKAGES,允许程序安装应用
//修改声音
android.permission.MODIFY_AUDIO_SETTINGS,修改声音设置信息
//录音
android.permission.RECORD_AUDIO,录制声音通过手机或耳机的麦克
//使用闪光灯
android.permission.FLASHLIGHT,允许访问闪光灯
//访问网络
android.permission.INTERNET,访问网络连接,可能产生GPRS流量
//改变wifi状态
android.permission.CHANGE_WIFI_STATE,打开wifi,改变wifi状态
//获取WiFi状态
android.permission.ACCESS_WIFI_STATE,获取当前WiFi接入的状态以及WLAN热点的信息
//获取网络状态
android.permission.ACCESS_NETWORK_STATE,获取网络信息状态,如当前的网络连接是否有效
//拍照权限
android.permission.CAMERA,允许访问摄像头进行拍照
//使用蓝牙
android.permission.BLUETOOTH,允许程序连接配对过的蓝牙设备
//电池状态
android.permission.BATTERY_STATS,允许应用程序获取电池状态的权限

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞