简体   繁体   English

设备打开时打开Android应用程序

[英]Open android application when device opens

I'm developing an android application that I want that starts when the device is opened and remains on foreground until it is closed. 我正在开发一个我想要的Android应用程序,它在设备打开时启动并保持在前台直到它关闭。 To achieve this I have used some tricks that I found surfing the Internet: 为了达到这个目的,我使用了一些我在网上冲浪的技巧:

  • Start application when device starts >> Solved using a broadcast receiver that handles the android.intent.action.BOOT_COMPLETED 设备启动时启动应用程序>>使用处理android.intent.action.BOOT_COMPLETED的广播接收器解决
  • Ignore the Home button when the application is opened >> Solved using an Activity Alias that is installed when the app starts and uninstalled when the application closes 打开应用程序时忽略“主页”按钮>>使用应用程序启动时安装的活动别名解决,并在应用程序关闭时卸载

I test the solution and it works fine almost always. 我测试解决方案,它几乎总能正常工作。 When I close the device with the app executing and it restarts the device the application is opened twice. 当应用程序执行时关闭设备并重新启动设备时,应用程序将打开两次。 I think that the extra opening is due because the Activity Alias wasn't uninstalled because the app doesn't close properly when I shutdown the device. 我认为额外的开放是因为没有卸载Activity Alias,因为当我关闭设备时应用程序没有正确关闭。

Is there a solution to avoid this behavior? 有避免这种行为的解决方案吗?

Thanks 谢谢

您应该使用ACTION_SHUTDOWN并在shutdown时执行必要的任务。在此处查看更多详细信息。

You can maybe use the Shutdown intent to finish your activity when the device is shutting down. 您可以使用Shutdown意图在设备关闭时完成您的活动。

<receiver android:name=".myReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
  </intent-filter>
</receiver>

Then you can just use the activity.finish(); 然后你可以使用activity.finish(); to close your app. 关闭你的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户在其设备+ Android中打开其他应用程序时,如何从我的应用程序执行某些操作 - How to perform some action from my App when the user opens some other application in his device + Android IBM Worklight-尝试在Android设备中打开应用程序时发生异常 - IBM Worklight - exception when trying to open application in Android device 如何感知用户何时打开 Android 应用程序 - How to Sense When the User Opens An Android Application 当我的 Android 应用程序的启动画面打开时,应用程序崩溃 - When the splash screen of my Android application opens the application crashes 当用户打开某个应用程序时启动我的应用程序 - Android - Launch my application when a user opens a certain application - Android 已安装Android应用程序,但无法在设备上打开 - Android Application Installed but won't open on device 单击主页或后退按钮时如何在android设备中打开我的应用程序 - How to open my application in android device when i click home or back button 当我的android应用程序打开时,仅获取一次当前位置 - Getting the current location exactly once when my android application opens SQLite数据库未在实际设备中打开,但已在模拟器中打开 - SQLite database is not open in real device but it opens in the emulator Android Deeplink 打开应用程序两次 - Android Deeplink opens application twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM