android-studio - How to automatically Start an activity at the same time for all clients?
问题描述
I am making an android game app with java in which users have options to register for a game and play the game when it starts at a time in the future (say game starts at 5:00 PM today). My problem is suppose 100 users have registered for the game that starts in at exact 5:00PM , then how to start the same activity/intent for all 100 users at 5:00 PM automatically if the user has opened the app? If the user has not opened the app I give him a notification "that game is about to begin, Kindly play your registered game." What should I use to accomplish this? Should I use Alarmmanager, job scheduler, Workmanager, Or Broadcast Receiver? And how to correctly use them for this.
解决方案
您可以使用前台服务。
此方法将帮助您从通知服务或其他服务自动打开应用程序
private void autoOpenApp() {
///----Auto open app----\\\
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "app:WakeLock");
wakeLock.acquire(10000);
Intent intent = new Intent(this, YOUR_ACTIVITY_WHICH_YOU_WANT_TO_OPEN.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
在标签内的清单中添加android:launchMode
和android:showOnLockScreen
<activity.../>
<activity
android:name="YOUR_ACTIVITY_WHICH_YOU_WANT_TO_OPEN"
android:launchMode="singleTop"
android:showOnLockScreen="true"
推荐阅读
- java - 仅在 QA 环境中而不是在本地获取“java.lang.NoClassDefFoundError:无法初始化类”?
- angular - Angular8 - 成功发布请求后重定向页面
- java - Flutter:如何在 Apple M1 芯片中安装 Flutter 及其所需的 SDK 元素?
- javascript - 使用 javascript/reactjs 从 MS 团队获取我的用户名
- mysql - 使用docker时mysql数据库保存在哪里?
- azure - Azure DevOps Server 本地构建代理没有响应
- ios - iOS URL Scheme 如何与 https 网站配合使用
- javascript - 登录使用电子邮件和密码创建的 Firebase 帐户时,密码消失了
- angular - 嗨,当从一个 Angular 应用程序切换到另一个应用程序时,如何让用户保持活动状态而不要求我再次登录?
- r - 在ggplot图例中隐藏未使用的级别