Farbik Redemy
Farbik Redemy

Reputation: 1

startActivity() in broadcast receiver bag(?)

I'm trying to start MainActivity with BluetoothDevice.ACTION_ACL_CONNECTED receive just like in some stack overflow answers

public class BTReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("BT", "Receive");
        String action = intent.getAction();
        ...
        switch (action) {
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                Intent i = new Intent();
                i.setClassName("com.opendashcam", "com.opendashcam.MainActivity");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

        }
    }
}

And like this

Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);

But all that I can see it's just these logs (first two say that receive was gotten and activity was started with connection to written MAC)

D/BT: Receive
D/BT: started app with 00:14:41:1E:26:27
I/Timeline: Timeline: Activity_launch_request time:129879532

My Main Activity's onCreate:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("MA", "Started app");
        init();
    }   

Upvotes: 0

Views: 77

Answers (1)

Shlomi Katriel
Shlomi Katriel

Reputation: 2403

Since Android 10, according to Android Developers docs ,Android does not allow launching an activity from the background:

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

As an alternative, you can show notification that will launch the activity if clicked:

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.

Upvotes: 1

Related Questions