Android 检测 手机硬件状态 的Utils

时间:2022-05-04 19:05:23
package com.cy.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.net.wifi.WifiManager;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Vibrator;
import android.widget.Toast;

@SuppressLint({"NewApi"})
public class CheckUtils
{
public static final int CILI_FLAG = 2;
public static final int JIASUDU_FLAG = 4;
public static final int NO_CILI_FLAG = 5;
public static final int NO_JIASUDU_FLAG = 7;
public static final int NO_TUOLUOYI_FLAG = 6;
public static final int TUOLUOYI_FLAG = 3;
private static int currVolume = 0;

/** 检测手电筒 */
public static boolean OpenLightOn(Context paramContext)
{
((SensorManager)paramContext.getSystemService("sensor"));
if (!paramContext.getPackageManager().hasSystemFeature("android.hardware.camera.flash"))
return false;
Camera localCamera = Camera.open();
Camera.Parameters localParameters = localCamera.getParameters();
localParameters.setFlashMode("torch");
localCamera.setParameters(localParameters);
localCamera.startPreview();
try
{
Thread.sleep(1000L);
localCamera.stopPreview();
localCamera.release();
if (localCamera != null)
return true;
}
catch (InterruptedException localInterruptedException)
{
while (true)
localInterruptedException.printStackTrace();
}
return false;
}

/** 打开扬声器 */
public static boolean OpenSpeaker(Context paramContext)
{
try
{
AudioManager localAudioManager = (AudioManager)paramContext.getSystemService("audio");
currVolume = localAudioManager.getStreamVolume(0);
if (!localAudioManager.isSpeakerphoneOn())
{
localAudioManager.setSpeakerphoneOn(true);
localAudioManager.setStreamVolume(0, localAudioManager.getStreamMaxVolume(0), 0);
}
return true;
}
catch (Exception localException)
{
localException.printStackTrace();
}
return false;
}

/** 检测蓝牙 */
public static boolean bluetoothIsGood()
{
BluetoothAdapter localBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!localBluetoothAdapter.isEnabled())
{
boolean bool2 = localBluetoothAdapter.enable();
localBluetoothAdapter.disable();
return bool2;
}
boolean bool1 = localBluetoothAdapter.disable();
localBluetoothAdapter.enable();
return bool1;
}

/** 检测相机 */
private static boolean checkCameraFacing(int paramInt)
{
if (getSdkVersion() < 9);
while (true)
{
return false;
int i = Camera.getNumberOfCameras();
Camera.CameraInfo localCameraInfo = new Camera.CameraInfo();
for (int j = 0; j < i; j++)
{
Camera.getCameraInfo(j, localCameraInfo);
if (paramInt == localCameraInfo.facing)
return true;
}
}
}

/** 检测定位 */
public static boolean getLocation(Context paramContext)
{
LocationManager localLocationManager = (LocationManager)paramContext.getSystemService("location");
Criteria localCriteria = new Criteria();
localCriteria.setAccuracy(1);
localCriteria.setAltitudeRequired(false);
localCriteria.setBearingRequired(false);
localCriteria.setCostAllowed(true);
localCriteria.setPowerRequirement(1);
String str = localLocationManager.getBestProvider(localCriteria, true);
if (str == null);
do
return false;
while (localLocationManager.getLastKnownLocation(str) == null);
return true;
}

/** 获取sdk版本 */
public static int getSdkVersion()
{
return Build.VERSION.SDK_INT;
}

/** 检测后置相机 */
public static boolean hasBackFacingCamera()
{
return checkCameraFacing(0);
}

/** 检测前置相机 */
public static boolean hasFrontFacingCamera()
{
return checkCameraFacing(1);
}

/** 检测加速器 */
public static void isAccelerometer(Context paramContext, Handler paramHandler)
{
SensorManager localSensorManager = (SensorManager)paramContext.getSystemService("sensor");
Sensor localSensor = localSensorManager.getDefaultSensor(1);
if (localSensor == null)
{
paramHandler.sendEmptyMessage(7);
return;
}
localSensorManager.registerListener(new SensorEventListener(paramHandler)
{
public void onAccuracyChanged(Sensor paramSensor, int paramInt)
{
}

public void onSensorChanged(SensorEvent paramSensorEvent)
{
CheckUtils.this.sendEmptyMessage(4);
}
}
, localSensor, 3);
}

/** 检测磁感 */
public static void isCiliGood(Context paramContext, Handler paramHandler)
{
SensorManager localSensorManager = (SensorManager)paramContext.getSystemService("sensor");
Sensor localSensor = localSensorManager.getDefaultSensor(2);
if (localSensor == null)
{
paramHandler.sendEmptyMessage(5);
return;
}
localSensorManager.registerListener(new SensorEventListener(paramHandler)
{
public void onAccuracyChanged(Sensor paramSensor, int paramInt)
{
}

public void onSensorChanged(SensorEvent paramSensorEvent)
{
CheckUtils.this.sendEmptyMessage(2);
}
}
, localSensor, 3);
}

/** 检测陀螺仪 */
public static void isGyroscopeGood(Context paramContext, Handler paramHandler)
{
SensorManager localSensorManager = (SensorManager)paramContext.getSystemService("sensor");
Sensor localSensor = localSensorManager.getDefaultSensor(4);
if (localSensor == null)
{
paramHandler.sendEmptyMessage(6);
return;
}
localSensorManager.registerListener(new SensorEventListener(paramHandler)
{
public void onAccuracyChanged(Sensor paramSensor, int paramInt)
{
}

public void onSensorChanged(SensorEvent paramSensorEvent)
{
CheckUtils.this.sendEmptyMessage(3);
}
}
, localSensor, 3);
}

/** 检测扬声器 */
public static boolean isSpeakerGood(Context paramContext)
{
return OpenSpeaker(paramContext);
}

/** 检测振动器 */
public static boolean isVibratorGood(Context paramContext)
{
Vibrator localVibrator = (Vibrator)paramContext.getSystemService("vibrator");
localVibrator.vibrate(1000L);
localVibrator.vibrate(new long[] { 100L, 10L, 100L, 1000L }, -1);
return localVibrator.hasVibrator();
}

/** 检测麦克风 */
public static boolean isMicrophoneGood(Context paramContext)
{
new MediaRecorder();
return paramContext.getPackageManager().hasSystemFeature("android.hardware.microphone");
}

/** 打开GPS */
public static void openGPSSettings(Activity paramActivity)
{
if (((LocationManager)paramActivity.getSystemService("location")).isProviderEnabled("gps"))
{
Toast.makeText(paramActivity, "GPS模块正常", 0).show();
return;
}
Toast.makeText(paramActivity, "请开启GPS!", 0).show();
paramActivity.startActivityForResult(new Intent("android.settings.SECURITY_SETTINGS"), 0);
}

/** 检测听筒 */
public static boolean isTelephoneGood(Context paramContext)
{
new MediaRecorder();
AudioManager localAudioManager = (AudioManager)paramContext.getSystemService("audio");
localAudioManager.setMode(2);
localAudioManager.setMode(0);
return true;
}

/** 检测Wifi */
public static boolean isWifiIsGood(Context paramContext)
{
WifiManager localWifiManager = (WifiManager)paramContext.getSystemService("wifi");
if (!localWifiManager.isWifiEnabled())
{
boolean bool2 = localWifiManager.setWifiEnabled(true);
localWifiManager.setWifiEnabled(false);
return bool2;
}
boolean bool1 = localWifiManager.setWifiEnabled(false);
localWifiManager.setWifiEnabled(true);
return bool1;
}

/** 关闭扬声器 */
public void CloseSpeaker(Context paramContext)
{
try
{
AudioManager localAudioManager = (AudioManager)paramContext.getSystemService("audio");
if ((localAudioManager != null) && (localAudioManager.isSpeakerphoneOn()))
{
localAudioManager.setSpeakerphoneOn(false);
localAudioManager.setStreamVolume(0, currVolume, 0);
}
return;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
}