0

I'm developing an application that needs to detect when the headphones are plugged in and no matter what are you doing in that moment, a PopUp appears with some information and two options like "open my app", "not now".

The problem is that the PopUp works if I'm in the application but doesn't work in any other screen...

This is my code actually when I detect the headphones:

Intent myIntent = new Intent(getApplicationContext(), PopUpActivity.class);
startActivity(myIntent);

and this is in the manifest.xml

<activity 
    android:name=".PopUpActivity" 
    android:launchMode="singleInstance" 
    android:excludeFromRecents="true" 
    android:taskAffinity="">
</activity>

My problem is that the Pop Up does not appear, but the detection of the headphones is working.

  • Possible duplicate of [How to detect when a user plugs headset on android device? (Opposite of ACTION\_AUDIO\_BECOMING\_NOISY)](https://stackoverflow.com/questions/13610258/how-to-detect-when-a-user-plugs-headset-on-android-device-opposite-of-action-a) – udit7395 Mar 30 '18 at 10:21
  • @udit7395 I've actually seen that post but this it not my problem. I'm detecting when the headphones are plugged in without a problem, I only need to display a pop up when this happens – Sergio Cervilla Mar 30 '18 at 10:23
  • The reason the pop-up doesn't show up because the activity where the broadcast has been registered has been killed. For your app to listen for such changes you must either run a foreground/background service depending on your targerted Android API to listen for such changes – udit7395 Mar 30 '18 at 10:28
  • @udit7395 This worked for me! I'm new to Android and i didn't know about services... Thanks a lot! – Sergio Cervilla Mar 30 '18 at 11:18
  • I am posting the comment as answer. Please mark it as accepted if it helped. Thanks. – udit7395 Mar 30 '18 at 12:46

1 Answers1

0

The reason the pop-up doesn't show up because the activity where the broadcast has been registered has been killed.

For your app to listen for such changes you must either run a foreground/background service depending on your targerted Android API to listen for such changes.

Hope this helps.

udit7395
  • 626
  • 5
  • 16