简体   繁体   English

Android-屏幕关闭或屏幕超时时终止应用

[英]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.

相关问题 创建在屏幕关闭时运行的应用程序 - Creating an app that runs when screen is turned off 屏幕关闭时,Android加速度计无法正常工作 - Android accelerometer not working when screen is turned off 即使在Android中关闭了屏幕,如何保持应用状态? - How to keep app status even if screen is turned off in Android? Android Xamarin 应用程序 - 如何在设备屏幕关闭然后重新打开时维护表格 state? - Android Xamarin app - how to maintain form state when device screen is turned off then back on? 如何在Android 5.0 Lollipop中关闭屏幕固定时收到通知? - How to be notified when screen pinning is turned off in Android 5.0 Lollipop? 屏幕关闭时,服务中的android加速计停止运行 - android accelerometer in service stops when screen is turned off 在Android中关闭屏幕时如何防止CPU“休眠”? - How to keep CPU from 'sleeping' when screen is turned off in Android? 屏幕关闭时调用Android onStart()事件 - Android onStart() event is called when the screen is turned off 是否可以在 JavaScript 中检测到 Android 和 iOS 浏览器中的屏幕何时关闭 - Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers 屏幕关闭时,Android BroadcastReceiver不起作用 - Android BroadcastReceiver does'nt work when screen is turned off
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM