Frage

I am currently working on a alarm application for android (yea i know im not the only one). I'm fairly new to Android Developing but I've managed to complete the core of the App and all i need is 1 thing. To be able to set an alarm (at x-y-z time) and once the alarm goes off, to start an activity EVEN IF PHONE IS ASLEEP(not off obviously), and the rest is done.

Now ive read the developers guide and i believe that the way to go is through a BroadcastReceiver and then should intentSender to the launch the activity but i cant seem where to find any examples or similar posts to atleast get an idea.

Right now Ive got the AlarmManager working actually waking to the activity but will only work if phone is completely awake and nothing at all if asleep, or at least til phone is unlocked.

Any suggestions? If required can post code example. Thanks in advance

Update

@Joel Thanks for the reply man. I had actually read about PowerManager.WakeLock, but glad u redirected me its way. Now this is what ive come up with for the receiving activity(it might be wrong but bear with me)

public class OnAlarmActivity extends Activity {

    MediaPlayer mpAlarm;
    MediaPlayer mpButton;
    PowerManager.WakeLock wl;

    private BroadcastReceiver theReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
            wl.acquire();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.on_alarm);

        mpAlarm = MediaPlayer.create(this, R.raw.filename);
        mpAlarm.start();
        mpButton = MediaPlayer.create(this, R.raw.buttonfilename);


        ImageView imgForAlarmScreen= (ImageView)findViewById(R.id.oftheimage);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
        imgForAlarmScreen.startAnimation(myFadeInAnimation); //animation for ImageView

        Button bNextActivity = (Button)findViewById(R.id.ofthebutton);
        bNextActivity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mpButton.start();
                startActivity(new Intent("com.myapps.otheractivity"));
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mpAlarm.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mpAlarm.pause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        mpAlarm.start();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        wl.release();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mpAlarm.release();
    }
}

Now all this and yet it will not awake nor even sound unless phone is active(awake). Any ideas on how i can effectively use the wake lock?

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top