Android 实现系统深度休眠笔记

文章介绍了如何在Android应用中检查并设置飞行模式,管理GPS状态,控制媒体声音,关闭相机以及与视频录制相关的操作。还强调了系统化学习的重要性,特别是对于有一定经验的Android工程师的成长路径和资源分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

然后执行对应的操作:

/**

  • 当前是否开启飞行模式

*/

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;

setup();

} else {

try {

mCamera.lock();

mCamera.setPreviewDisplay(mHolder);

mCamera.startPreview();

mCamera.unlock();

} catch (Exception e) {

// e.printStackTrace();

总结

其实要轻松掌握很简单,要点就两个:

  1. 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
  2. 多练。 (视频优势是互动感强,容易集中注意力)

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。**

[外链图片转存中…(img-anO4jW9i-1714313920128)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值