Android 实现系统深度休眠笔记,app保活双进程守护

Android 实现系统深度休眠笔记,app保活双进程守护,第1张

Android 实现系统深度休眠笔记,app保活双进程守护
  • DC连接汽车12V永不掉电,熄火时ACC发出掉电信号时,行车记录装置采用不关机,深度休眠策略。关闭屏幕,停止录像,记录轨迹的同时,需要打开飞行模式(蓝牙,WiFi),关闭FM发射,关闭GPS。如果此时有音乐播放和后台导航,也需要关闭。

  • 深度休眠时,待机电流降到10-30mA,此时底层摄像头已不再断电,所以在此步骤进行之前,要停掉Camera预览。否则机器唤醒的时候预览区域会出现卡死。

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

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);

}

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

publ

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ic void killAppByPackageName(String package){

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

List 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;

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5612558.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存