- 关闭屏幕,发送自定义广播:
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;
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
345819577)]
[外链图片转存中…(img-TmKtzeOv-1715345819579)]
[外链图片转存中…(img-OJOP8tED-1715345819580)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!