Start Activity Inside Onreceive Broadcastreceiver

Start Activity inside onReceive BroadcastReceiver

You have context passed as parameter to onRecieve() method, so just use:

 @Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}

It works, of course you have to change package and activity class name to your own.

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

Trying to start an activity from broadcast receiver

Why are you not starting simple intent like this..

startActivity(new Intent(this, LockScreenActivity.class));
finish();

or you can try this..

Intent i = new Intent(context,LockScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Can't start activity from BroadcastReceiver on android 10

Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.

Use a high-priority notification, with an associated full-screen Intent, instead. See the documentation. This sample app demonstrates this, by using WorkManager to trigger a background event needing to alert the user. There, I use a high-priority notification instead of starting the activity directly:

val pi = PendingIntent.getActivity(
appContext,
0,
Intent(appContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)

val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(pi, true)

val mgr = appContext.getSystemService(NotificationManager::class.java)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
) {
mgr.createNotificationChannel(
NotificationChannel(
CHANNEL_WHATEVER,
"Whatever",
NotificationManager.IMPORTANCE_HIGH
)
)
}

mgr.notify(NOTIF_ID, builder.build())

startActivity() from BroadcastReceiver

If your goal is that you want NightClock to be started whenever an ACTION_POWER_CONNECTED broadcast is sent, your approach of using a BroadcastReceiver is fine. However, do not register it from an activity. Rather, register it in the manifest:

<receiver android:name=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>

Then, have your BroadcastReceiver as a public Java class (here named OnPowerReceiver, though you can call it whatever you want), and have it call startActivity().

Bear in mind that users probably do not want you doing this. There are many other cases for connecting a phone to power besides starting a "night clock". I humbly suggest you simply let users start your activity via the home screen.

BroadcastReceiver OnReceive not called when returning from StartActivity(ActivityFlags.NewTask)

SOLVED!
Apparently it does not work when LaunchMode of the activity is set to SingleInstance.

How to start activity from BroadcastReceiver onReceive method?

Call startActivity() on the Context supplied to onReceive(), including Intent.FLAG_ACTIVITY_NEW_TASK in the flags of the Intent you pass to startActivity().

Note that starting an activity without the user initiating it may be considered poor form by the user.



Related Topics



Leave a reply



Submit