[英]Android - Kill an app when screen turned off or screen times out
I want to kill / fully close an app so it doesn't run even in the background when I press the screen turn on/off button or if the screen times out. 我想杀死/完全关闭一个应用程序,这样当我按下屏幕打开/关闭按钮或屏幕超时时,它甚至不会在后台运行。 I couldn't find a solution anywhere on the internet.
我在互联网上的任何地方都找不到解决方案。 Can you guys help me out with a code snippet?
你们可以用代码片段帮助我吗? Thanks
谢谢
you can refer this link to detect screen turn off Screen off Broadcast receiver and for killing the app you can use below code 您可以参考此链接来检测屏幕是否关闭屏幕关闭广播接收器 ;要杀死该应用程序,您可以使用以下代码
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
first check if the screen is locked inside a service that runs in the background: 首先检查屏幕是否锁定在后台运行的服务中:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
getActivity().finish();
System.exit(0);
} else {
//it is not locked
}
then you simply kill the app if the screen is locked. 那么您只需在屏幕锁定的情况下杀死该应用即可。 hope this will help.
希望这会有所帮助。
To make activity like a toast (appear-and-go) add following code into manifest: 为了使活动像吐司一样(随处可见),将以下代码添加到清单中:
<activity android:name=".YourActivity"
android:label="YourActivityLabel"
android:taskAffinity=""
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:noHistory="true"
android:launchMode="singleTask">
</activity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.