欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android常用小功能

程序员文章站 2022-06-05 16:12:17
...

在项目开发中有用到一些系统小功能,在此记录一下。
Note: 以下方法我是写在PhoneWindowManager.java中的,其他地方执行相同代码可能需要申请一下相应的权限。

1. 开启关闭手电筒

    private boolean hasClosed = true;
    private CameraManager mCameraManager;
    private String mCameraId;
    private void controlFlashlight(){
    	mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
    	try {
            mCameraId = getCameraId();
        } catch (Throwable e) {
            Log.e(TAG, "Couldn't initialize.", e);
            return;
        }
    	if (mCameraId == null) {
    		return;
    	}
    	if (hasClosed) {
    		try {
                    //开启手电
                    mCameraManager.setTorchMode(mCameraId, true);
                    hasClosed = false;
                } catch (CameraAccessException e) {
                    Log.e(TAG, "Couldn't set torch mode", e);
                }
        } else {
            try {
                    //关闭手电
                    mCameraManager.setTorchMode(mCameraId, false);
                    hasClosed = true;
                } catch (CameraAccessException e) {
                    Log.e(TAG, "Couldn't set torch mode", e);
                }
        }
    }

    /**
    *获取可用的CameraID
    *开启手电需要CameraID
    */
    private String getCameraId() throws CameraAccessException {
        String[] ids = mCameraManager.getCameraIdList();
        for (String id : ids) {
            CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
            Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
            Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
            if (flashAvailable != null && flashAvailable
                    && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                return id;
            }
        }
        return null;
    }

2. 检测某个APP是否运行在前台
需传入APP的包名

  private boolean isAppRunning(String packageName) {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        //getRunningTasks(num); num值可根据需求调整
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list.size() <= 0) {
            return false;
        }
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(packageName)) {
                return true;
            }
        }
        return false;
    }

3. 检测设备是否处于锁屏状态

   private boolean isOnLockScreenPage(){
    	KeyguardManager mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
    	return mKeyguardManager.inKeyguardRestrictedInputMode();
    }

4. 点亮屏幕

   private void lightScreen(){
        PowerManager mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    	if (!mPowerManager.isScreenOn()) {
    		mPowerManager.wakeUp(SystemClock.uptimeMillis());	
    	}
    }
相关标签: android 系统