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?

user3429953
  • 352
  • 3
  • 19

2 Answers2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I ended up using a full screen notification with high-priority and then it shows even if the screen is locked. – user3429953 Sep 25 '19 at 17:13
  • @commonsware and how about alarm/reminder apps which absolutely crucially MUST launch an activity from background to play alarm? Notifications are not really solution here – qkx Oct 31 '19 at 11:00
  • @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. – CommonsWare Oct 31 '19 at 11:32
  • Thats not solution, thats not how alarms are supposed to work. Alarms are supposed to be full screen activity, playing sound. So its not possible to make an alarm app anymore for android? – qkx Nov 02 '19 at 06:32
  • @qkx: "thats not how alarms are supposed to work" -- that is your opinion. "So its not possible to make an alarm app anymore for android?" -- that depends on one's definition of "alarm app", I suppose. For your definition, no, one cannot create an alarm app anymore. – CommonsWare Nov 02 '19 at 10:56
  • 1
    From the [documentation](https://developer.android.com/guide/components/activities/background-starts), it seems that if the user grants the "display over other apps" permission, the app would be exempt from the restriction. – Jack Jun 24 '20 at 07:04
  • @user3429953. Could you walk me through how you managed this? – Adesuyi Ayodeji Mar 25 '21 at 08:08
  • 1
    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://stackoverflow.com/questions/37679266/fullscreen-notification/43830658#43830658 and for me it was enough... – user3429953 Jul 01 '21 at 12:50
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())
        }
    }
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • Thanks a lot for the example code. Yeah, I think that's the best option due to the new restrictions (it's the same that I thought to do if there is no possibility to reproduce the current behaviour). – user3429953 Aug 14 '19 at 12:08
  • 3
    But how whatsapp is working. I get a call when my app is closed? – Rahul Giradkar Jan 10 '20 at 11:10
  • Just think a bit, how can I show SMS popup from notification?!! – Amir Hossein Ghasemi Apr 26 '20 at 15:08
  • @RahulGiradkar I think the whatsapp solution is a Fullscreen Notification. Check this post https://stackoverflow.com/a/43830658/3429953 – user3429953 Jul 16 '20 at 13:50
  • nope, i found skype can display calling screen directly when user got a call, even screen is off + skype is not running – famfamfam Sep 22 '20 at 09:01
  • 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). – user3429953 Jan 14 '22 at 18:49