Start activity from receiver in Android Q
Asked Answered
A

2

5

I'm checking my app with the Android Q [beta 6] in order to add all the required changes to be fully-compatible with the last SO. However, I found out that I am using a Receiver to start an Activity from background and due to the last background limitations implemented (https://developer.android.com/preview/privacy/background-activity-starts) the activity is not being opened.

I tried to use both the receiver context and application context to start the activity but in both cases the system shows a toast saying that is not possible to start activity from background.

What I tried on the Receiver...

class MyReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        context?.applicationContext?.let {
            it.startActivity(Intent(it, MyActivity::class.java).apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
            PushUtils.showReceiverCalledNotification(it)
        }
    }

That way I wanted to start MyActivity and also show a notification when the receiver is called. Instead, I can see the notification but the Activity is never started. It is very important for the feature to start the activity immediately, so there is a way to continue starting the activity from the receiver?

Ashaashamed answered 14/8, 2019 at 11:25 Comment(2)
Did you try with the SYSTEM_ALERT_WINDOW permission?Thermionic
Yes. Same problem when calling startActivity.Ashaashamed
H
4

It is very important for the feature to start the activity immediately, so there is a way to continue starting the activity from the receiver?

No, sorry. Use a high-priority notification, so it appears in "heads-up" mode. The user can then rapidly tap on it to bring up your activity.

Hardworking answered 14/8, 2019 at 11:28 Comment(5)
I ended up using a full screen notification with high-priority and then it shows even if the screen is locked.Ashaashamed
@commonsware and how about alarm/reminder apps which absolutely crucially MUST launch an activity from background to play alarm? Notifications are not really solution hereCommutative
@qkx: A heads-up notification will directly display an activity (via the "full-screen Intent) if the display was off at the time the notification was raised. If the display was on, a heads-up notification will display a bubble for a few seconds, because the user is using the device, and it may not be safe to interrupt what they are doing.Hardworking
From the documentation, it seems that if the user grants the "display over other apps" permission, the app would be exempt from the restriction.Proportional
Hello @AdesuyiAyodeji, I could not open an Activity due to the new background restrictions, but what I could perform was to build a Full-Screen notification that works exactly like CommonsWare posted on the comments. I follow this steps: https://mcmap.net/q/1016878/-fullscreen-notification/… and for me it was enough...Ashaashamed
T
3

Due to restrictions, you cannot start activity from background. Instead you can use notifications as CommonsWare suggested and also suggested on the android developer site.

Here's the official documentation that lists situations when it will work and when won't.

https://developer.android.com/guide/components/activities/background-starts

You can use something like this:

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        context ?: return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            showNotification(context.applicationContext)
        } else {
            context.applicationContext.startActivity(Intent(context, MyActivity::class.java).apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
        }
        PushUtils.showReceiverCalledNotification(context)

    }

    private fun showNotification(context: Context) {
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("default", "default", NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }

        val intent = Intent(context, MyActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }

        val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT)

        with(NotificationCompat.Builder(context, "default")) {
            setSmallIcon(R.drawable.ic_scan_colored)
            setContentTitle("Custom Title")
            setContentText("Tap to start the application")
            setContentIntent(pendingIntent)
            setAutoCancel(true)
            manager.notify(87, build())
        }
    }
}
Tennes answered 14/8, 2019 at 11:41 Comment(5)
But how whatsapp is working. I get a call when my app is closed?Cyan
Just think a bit, how can I show SMS popup from notification?!!Benthos
@RahulGiradkar I think the whatsapp solution is a Fullscreen Notification. Check this post https://mcmap.net/q/1016878/-fullscreen-notificationAshaashamed
nope, i found skype can display calling screen directly when user got a call, even screen is off + skype is not runningFloorboard
But this method is compatible with what you said about Skype. I think Skype should send a push notification of "data message" type when a call is initiated, so the second user app can manage to display the full screen notification by itself. And then from the Activity set on the PendingIntent of the notification it should be able to manage the retrieved data to perform the P2P call (I mean, it's only a guess but I think it should be possible).Ashaashamed

© 2022 - 2024 — McMap. All rights reserved.