首页 > 解决方案 > 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.

标签: android-studioandroid-activitybroadcastreceiverandroid-workmanagerandroid-jobscheduler

解决方案


您可以使用前台服务

此方法将帮助您从通知服务或其他服务自动打开应用程序

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:launchModeandroid:showOnLockScreen<activity.../>

    <activity
        android:name="YOUR_ACTIVITY_WHICH_YOU_WANT_TO_OPEN"
        android:launchMode="singleTop"
        android:showOnLockScreen="true"

推荐阅读