My music stop when I press home button but when the screen is off it is playing. How can I stop this? And restore when the screen is on.
Asked
Active
Viewed 933 times
-3
-
1stop the music in `onPause`. restore it in `onResume` – Raghunandan May 30 '14 at 15:31
-
yes but if I do this music will be stop when I open new Activity. I don't want this. – Seravier May 30 '14 at 15:33
-
then what do you want? What do you mean by screen is off? – Raghunandan May 30 '14 at 15:33
-
When I play power button and screen goes off I want that musis also stop – Seravier May 30 '14 at 15:35
-
screen lock an unlock?? – Raghunandan May 30 '14 at 15:36
-
yes if screen is lock, music stop – Seravier May 30 '14 at 15:37
-
This question appears to be off-topic because it is not about programming. See the [Help Center](http://stackoverflow.com/help/asking) for details on what makes a good question. – Andrew May 30 '14 at 15:37
-
2This question appears to be off-topic because it is not about programming. – kjhughes May 30 '14 at 16:10
2 Answers
0
Create a service in background, create a single instance of soundPlayer. In the service check which is the Activity in focus. If the package does not belong to your Application, then stop the music.

Shishir Shetty
- 2,021
- 3
- 20
- 35
0
You need to programmatically register a BroadcastReceiver
to receive the ACTION_SCREEN_OFF
and ACTION_SCREEN_ON
intent actions. You can not register a receiver in your AndroidManifest.xml for these actions.
private BroadcastReceiver screenOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action) {
// do stuff
}
}
};
// to register the receiver
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
context.registerReceiver(screenOffReceiver, filter);
Hopefully you are playing music in a Service
, in which case you can register the screen off receiver when your music starts. Service
extends Context
, so you can call registerReceiver
directly.

Karakuri
- 38,365
- 12
- 84
- 104