how open activity from ForegroundService when phone is locked and screen is off in android 10?

653 views Asked by At

as is clear from my question I want start activity from foreground service. in android 5-9 it will start but in android 10 has restrictions and I do not know what to do!

this is activity that need to be started:

public class Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_time);

        // Important: have to do the following in order to show without unlocking
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }
}

and manifest:

<activity android:name=".Activity"
    android:showOnLockScreen="true"
    android:screenOrientation="portrait"
    android:autoRemoveFromRecents="true"
    android:excludeFromRecents="true"
/>
1

There are 1 answers

0
Kishan Thakkar On

Starting from android 10 in most cases it is not possible to open your activity from background service as they have added many restrictions to provide better experience to user .

Here are the Exceptions to the restriction

The app has a visible window, such as an activity in the foreground. The app has an activity in the back stack of the foreground task. The app has an activity in the back stack of an existing task on the Recents screen.

The app called finish() on an activity very recently. This applies only when the app had either an activity in the foreground or an activity in the back stack of the foreground task at the time finish() was called.

The app has a service that is bound by the system. This condition applies only for the following services, which might need to launch a UI: AccessibilityService, AutofillService, CallRedirectionService, HostApduService, InCallService, TileService, VoiceInteractionService, and VrListenerService.

The app has a service that is bound by a different, visible app. Note that the app that is bound to the service must remain visible for the app in the background to start activities successfully.

The app receives a notification PendingIntent from the system. In the case of pending intents for services and broadcast receivers, the app can start activities for a few seconds after the pending intent is sent.

The app receives a PendingIntent that is sent from a different, visible app.

The app receives a system broadcast where the app is expected to launch a UI. Examples include ACTION_NEW_OUTGOING_CALL and SECRET_CODE_ACTION. The app can start activities for a few seconds after the broadcast is sent.

The app is associated with a companion hardware device through the CompanionDeviceManager API. This API allows the app to start activities in response to actions that the user performs on a paired device.

The app is a device policy controller running in device owner mode. Example use cases include fully managed enterprise devices, as well as dedicated devices like digital signage and kiosks.

The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.

Here is reference link for same Exceptions to the restriction