Android 实现系统深度休眠笔记

  • 关闭屏幕,发送自定义广播:

context.sendBroadcast(new Intent(“tchip.intent.action.ACTION_KEY_POWER”));

接收的应用,需要具备INJECT_EVENTS权限:

和系统的userId:

android:sharedUserId=“android.uid.system”

接收到此广播后,发出对应的key即可:

sendKeyCode(KeyEvent.KEYCODE_POWER);

  • 打开/关闭飞行模式,同样发送自定义广播给拥有系统uid的应用,同时需要具备权限写入WRITE_SECURE_SETTINGS,打开setting.db可以看到三个表,其中secure表是一些敏感字段:

这里写图片描述

这里写图片描述

然后执行对应的操作:

/**

  • 当前是否开启飞行模式

*/

private boolean isAirplaneModeOn(Context context) {

// 返回值是1时表示处于飞行模式

int modeIdx = Settings.Global.getInt(context.getContentResolver(),

Settings.Global.AIRPLANE_MODE_ON, 0);

boolean isEnabled = (modeIdx == 1);

//MyLog.v(“[SleepReceiver]isAirplaneModeOn:” + isEnabled);

return isEnabled;

}

/**

  • 设置飞行模式

*/

private void setAirplaneMode(boolean setAirPlane, Context context) { Settings.Global.putInt(context.getContentResolver(),

Settings.Global.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);

// 广播飞行模式的改变,让相应的程序可以处理。

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);

intent.putExtra(“state”, setAirPlane);

context.sendBroadcast(intent);

}

  • 根据包名杀死后台应用:

public void killAppByPackageName(String package){

ActivityManager myActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

List<ActivityManager.RunningAppProcessInfo> mRunningPros = myActivityManager.getRunningAppProcesses();

for (ActivityManager.RunningAppProcessInfo amPro : mRunningPros){

if(amPro.processName.contains(package)){

try {

Method forceStopPackage = myActivityManager.getClass().getDeclaredMethod(“forceStopPackage”, String.class);

forceStopPackage.setAccessible(true);

forceStopPackage.invoke(myActivityManager, amPro.processName);

}

catch (Exception e) {

}

}

}

  • 媒体/铃声声音静音,需要保存休眠前的音量,供唤醒后恢复:

if (Constant.Module.muteWhenSleep) {

int volumeMusic = audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC);

int volumeRing = audioManager .getStreamVolume(AudioManager.STREAM_RING);

editor.putInt(“volumeMusic”, volumeMusic);

editor.putInt(“volumeRing”, volumeRing);

editor.commit();

}

  • 关闭/打开GPS

context.sendBroadcast(new Intent(

“tchip.intent.action.ACTION_GPS_OFF”));

private static boolean getGpsState(Context context) {

ContentResolver resolver = context.getContentResolver();

boolean gpsState = Settings.Secure.isLocationProviderEnabled(resolver,

LocationManager.GPS_PROVIDER);

Log.v(“ZMS”, “[GPS]Now State:” + gpsState);

return gpsState;

}

private void setGpsState(Context context, boolean isGpsOn) {

ContentResolver resolver = context.getContentResolver();

boolean nowState = getGpsState(context);

if (isGpsOn != nowState) {

Log.v(“ZMS”, “[GPS]Set State:” + isGpsOn);

Settings.Secure.setLocationProviderEnabled(resolver,

LocationManager.GPS_PROVIDER, isGpsOn);

}

}

  • 停止录像预览,释放recorder:由于熄屏时,不会触发SurfaceView的surfaceDestroy,所以将destroy的过程移动到Activity的onPause中执行

private void releaseCameraZone() {

release();

// mHolder = null;

if (mCamera != null) {

mCamera.stopPreview();

}

MyApplication.shouldResetRecordWhenResume = true;

}

public void release() {

releaseRecorder();

closeCamera();

}

private void releaseRecorder() {

if (mMyRecorder != null) {

mMyRecorder.stop();

mMyRecorder.close();

mMyRecorder.release();

mMyRecorder = null;

MyLog.d(“Record Release”);

}

}

private boolean closeCamera() {

if (mCamera == null)

return true;

try {

mCamera.lock();

mCamera.stopPreview();

mCamera.setPreviewDisplay(null);

mCamera.release();

mCamera.unlock();

mCamera = null;

return true;

} catch (Exception ex) {

mCamera = null;

return false;

}

}



唤醒

  • 摄像头预览区域重新绘制,在Activity的onResume中判断是否需要执行:

if (!MyApplication.isFirstLaunch) {

if (!MyApplication.isVideoReording

|| MyApplication.shouldResetRecordWhenResume) {

MyApplication.shouldResetRecordWhenResume = false;

// 重置预览区域

if (mCamera == null) {

// mHolder = holder;

结尾

最后,针对上面谈的内容,给大家推荐一个Android资料,应该对大家有用。

首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)

泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶
注解原理丶注解原理丶序列化
Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)
代码框架结构优化(数据结构丶排序算法丶设计模式)
APP性能优化(用户体验优化丶适配丶代码调优)
热修复丶热升级丶Hook技术丶IOC架构设计
NDK(c编程丶C++丶JNI丶LINUX)
如何提高开发效率?
MVC丶MVP丶MVVM
微信小程序
Hybrid
Flutter

接下来是资料清单:(敲黑板!!!


1.数据结构和算法

2.设计模式

3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记

4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)

不论遇到什么困难,都不应该成为我们放弃的理由!共勉~

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
14414966475)]

3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记

[外链图片转存中…(img-VKp0aBtW-1714414966477)]

4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)

[外链图片转存中…(img-aarMseO7-1714414966479)]

不论遇到什么困难,都不应该成为我们放弃的理由!共勉~

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-c05KqHlx-1714414966481)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 30
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值