Android Broadcastreceiver on Startup - Keep Running When Activity Is in Background

How to start an activity from BroadcastReceiver's onReceive() method when app is in background?

As you commented out you are trying to launch an Activity when your App is in Background on Android 10 (API level 29).

From Android (API level 29) they put some restrictions to open an Activity when your App is in Background.

Android 10 (API level 29) and higher place restrictions on when apps
can start activities when the app is running in the background.

You can find out here Restrictions on starting activities from the background.

They have also mentioned that

In nearly all cases, apps that are in the background should display
time-sensitive notifications to provide urgent information to the user
instead of directly starting an activity. Examples of when to use such
notifications include handling an incoming phone call or an active
alarm clock.

So to overcome this behavior instead of calling your App when it is in the background you should show high-priority notification with a full-screen intent.

For more information on High-Priority Notification and Full-Screen Intent, you can check it here Display time-sensitive notifications

Long running background task from BroadcastReceiver - which class to use?

goAsync() return a PendingIntent - it mean you ask for android system extend time life of Broadcast receiver => goAsync() is used for short background task.
Life time of BroadcastReceiver is very short, so... for long time background task, you must to change to other context has longer life time, such as: Service, JobService..etc.
Example:

  1. BroadcastReceiver received intent
  2. BroadcastReceiver start a service, run worker thread to process long time task
  3. after worker thread finish, call finish service too

=========================================

class MyIntentService : Service() {
private val handleThread = HandlerThread("MyThread")
private lateinit var workerHandler: Handler
override fun onCreate() {
super.onCreate()
handleThread.start()
workerHandler = Handler(handleThread.looper)
}

override fun onDestroy() {
workerHandler.removeCallbacksAndMessages(null)
handleThread.quitSafely()
super.onDestroy()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val data = intent?.data
workerTask(data)
return START_NOT_STICKY
}

private fun workerTask(data: Uri?) {
workerHandler.post {
heavyTask(data)
finishMyIntentService()
}
}

private fun finishMyIntentService() {
stopSelf()
}

private fun heavyTask(data: Uri?) {
// to do heavyTask example
for (i in 1..20)
{
Log.d("test","#heavyTask() $i")
Thread.sleep(1000)
}
}

override fun onBind(intent: Intent?): IBinder? {
TODO("Not yet implemented")
}
}

then startService from BroadCastReceiver

Keep broadcast receiver running after application is closed

You can use a service

In main app start/stop the service

Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);

service

public class MyService extends Service
{
private static BroadcastReceiver m_ScreenOffReceiver;

@Override
public IBinder onBind(Intent arg0)
{
return null;
}

@Override
public void onCreate()
{
registerScreenOffReceiver();
}

@Override
public void onDestroy()
{
unregisterReceiver(m_ScreenOffReceiver);
m_ScreenOffReceiver = null;
}

private void registerScreenOffReceiver()
{
m_ScreenOffReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "ACTION_SCREEN_OFF");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(m_ScreenOffReceiver, filter);
}
}

Start activity from receiver in Android Q

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.



Related Topics



Leave a reply



Submit