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();
}
}
}