Skip to content

Instantly share code, notes, and snippets.

@Flywith24
Last active July 1, 2020 11:37
Show Gist options
  • Save Flywith24/35b376bc3f5d7e5b2a440e6cb5e9649e to your computer and use it in GitHub Desktop.
Save Flywith24/35b376bc3f5d7e5b2a440e6cb5e9649e to your computer and use it in GitHub Desktop.
Framework 层常用定制功能 Gist
这里记录了 Framework 定制的常用方法
fun screenCapture(context: Context) {
val screenPath =
"${context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath}/ScreenCaps/"
val path = "$screenPath/ScreenCap_${System.currentTimeMillis()}.png"
val file = File(screenPath)
if (!file.exists()) {
file.mkdir()
}
Runtime.getRuntime().exec("screencap -p $path")
}
//清空全部
fun clearAll(){
// 9.0 后删除了 ActivityManager 的 removeTask() 方法
//import android.app.ActivityTaskManager.getService
//清空最近任务
getService().removeAllVisibleRecentTasks()
}
//根据包名清理
fun clearPackage(packageName : String){
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val recentTasks = manager.getRecentTasks(Int.MAX_VALUE, ActivityManager.RECENT_WITH_EXCLUDED)
recentTasks.forEach { task ->
if(packageName == task.baseIntent.component?.packageName){
getService().removeTask(task.taskId)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment