0

I was trying to write a code where everytime a user presses power on button and brings mobile back from sleep it should play a sound if the app is running else nothing happens.

I guess I need a broadcast Receiver where it checks it the power:ON was pressed and not Power:OFF and plays a sound . Which later will be relaced with async task .

How do I achieve the above requirement . Please suggest me some method.
I dont want to use service ,as it keeps running even if the the app is not running .

And I want it to run on only if the the app is running in the background hence BroadCast Receiver.

I am noob to android.
Please Help.
Thanks in Advance.

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {


    MediaPlayer mp3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mp3=MediaPlayer.create(this, R.raw.sound);


    }






    public class YourReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // do what you want when the screen is turned back on

            mp3.start();
        }
    }

}
James Patrick
  • 273
  • 4
  • 19

1 Answers1

1

You can register a BroadcastReceiver in your manifest file to listen for when the power button is pressed. This will tell the system that you have a class called your.package.YourReceiver that wants to do something when the power button is pressed to turn the screen on.

<receiver android:name="your.package.YourReceiver">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_ON"></action>
    </intent-filter>
</receiver>

And then you have to create a class to handle the event. This is the code that will run when the broadcast is received.

public class YourReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // do what you want when the screen is turned back on
    }
}

NOTE...

If you need to handle a power button press that turns the screen OFF use this in your manifest.

<action android:name="android.intent.action.SCREEN_OFF"></action>

You can use one or the other or both.

Jason Crosby
  • 3,533
  • 4
  • 28
  • 49
  • Thanks Jason . I have edited the entire code as above .I dont know how to call that class .Please Help. – James Patrick Feb 20 '13 at 18:51
  • I am sorry for the trouble but I really appreciate the help. – James Patrick Feb 20 '13 at 19:04
  • The system will call the `onRecieve()` method automatically for you when the user clicks the power button – Jason Crosby Feb 20 '13 at 23:45
  • Thank You Sir.I will replace the entire code now with the working code. I tried the code and it works for a while ,when I press the home button and the app goes in the background and after an hour or so if I turn back on to check any notifications it does not play the sound above ? why ? Is the the app killed by android os ? How Do I fix it Sir ? Thanks for your time. – James Patrick Feb 21 '13 at 08:23
  • If this isn't fully solving your problem, you can check out the answers here http://stackoverflow.com/questions/3703071/how-to-hook-into-the-power-button-in-android . There are some other good suggestions there. – Jason Crosby Feb 21 '13 at 15:57
  • Thank you Sir its working now I am using ACTION_USER_PRESENT its working fine anther quick add-on question.How to add asynctask within on receive ? – James Patrick Feb 21 '13 at 19:29
  • 1
    Just call the `AsyncTask` `execute()` method in the `onReceive()` and that should run your code – Jason Crosby Feb 21 '13 at 23:56