android - Activity start from sleep mode immediately close? -
android - Activity start from sleep mode immediately close? -
i have app wich kind of alarmclock. have weird issue.
the principle simple : alarmmanager send broadcast start service , activity.
the service vibrates phone 10s , kill activity , itself. activity shows dismiss button. if click on it, stops service , itself.
if broadcast received when the phone on, works fine (activity starts , phone vibrates).
if broadcast received when phone is in sleeping mode, then activity starts , immediatly stops (you can't see on screen unless set thread.sleep somewhere). service works fine.
i don't understand why activity stops after beingness created ?
in logcat, have these 2 lastly lines didn't have when it's working :
06-23 18:07:58.349: i/activitymanager(305): displayed com.example.testproject/.alarmescreenoffactivity: +100ms 06-23 18:07:58.349: i/power(305): *** set_screen_state 1 06-23 18:07:58.359: d/kernel(145): [557082.194793] request_suspend_state: wakeup (3->0) @ 557080649502843 (2014-06-23 16:07:58.358764661 utc)
thanks.
here's code in receiver :
wakelockmanager.acquirewakelock(context); intent closedialogs = new intent(intent.action_close_system_dialogs); context.sendbroadcast(closedialogs); intent alarmeactivity = new intent(context, alarmescreenoffactivity.class); alarmeactivity.setflags(intent.flag_activity_new_task); context.startactivity(alarmeactivity); intent playalarm = new intent(context, alarmesonvibration.class); context.startservice(playalarm);
the activity :
private broadcastreceiver mreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { if (mybroadcastreceiver.alarm_timeout.equals(intent.getaction())) { dismiss(); } } }; @override protected void oncreate(bundle icicle) { super.oncreate(icicle); updatelayout(); registerreceiver(mreceiver, new intentfilter(mybroadcastreceiver.alarm_timeout)); } private void updatelayout() { requestwindowfeature(android.view.window.feature_no_title); this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen | windowmanager.layoutparams.flag_dismiss_keyguard | windowmanager.layoutparams.flag_show_when_locked | windowmanager.layoutparams.flag_turn_screen_on, windowmanager.layoutparams.flag_fullscreen | windowmanager.layoutparams.flag_dismiss_keyguard | windowmanager.layoutparams.flag_show_when_locked | windowmanager.layoutparams.flag_turn_screen_on); setcontentview(r.layout.alarm_alert); button dismissbouton = (button) findviewbyid(r.id.dismiss); dismissbouton.settext("dismiss"); dismissbouton.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { dismiss(); } }); } private void dismiss() { intent playalarm = new intent(this, alarmesonvibration.class); stopservice(playalarm); finish(); } @override public void ondestroy() { super.ondestroy(); unregisterreceiver(mreceiver); }
and service :
private static int alarm_timeout_seconds = 10; private static final long[] svibratepattern = new long[] { 500, 300 }; private boolean mplaying = false; private vibrator mvibrator; private static final int timeout = 1001; private handler mhandler = new handler() { public void handlemessage(message msg) { switch (msg.what) { case timeout: sendtimeoutbroadcast(); stopself(); break; } } }; @override public void oncreate() { wakelockmanager.acquirewakelock(this); mvibrator = (vibrator) getsystemservice(context.vibrator_service); } @override public void ondestroy() { super.ondestroy(); stop(); wakelockmanager.releasewakelock(); } @override public int onstartcommand(intent intent, int flags, int startid) { if (intent == null) { stopself(); homecoming start_not_sticky; } play(); homecoming start_sticky; } private void sendtimeoutbroadcast() { intent alarmtimeout = new intent(mybroadcastreceiver.alarm_timeout); sendbroadcast(alarmtimeout); } private void play() { stop(); mvibrator.vibrate(svibratepattern, 0); enabletimeout(); mplaying = true; } public void stop() { if (mplaying) { mplaying = false; mvibrator.cancel(); } disabletimeout(); } private void enabletimeout() { mhandler.sendmessagedelayed(mhandler.obtainmessage(timeout, true), 1000 * alarm_timeout_seconds); } private void disabletimeout() { mhandler.removemessages(timeout); }
edit :
after few other tests, appears without starting service, app doesn't want activity remain on ui.
the broadcast triggers activity , device wakes , run activity, shuts downwards (it goes onresume onpause immediatly).
with or whithout keyguard, result same, activity starts , stops.
it should simple ? seek run activity sleep mode trigering broadcast.
ok, solve issue if don't understand what's happening.
in activity started sleep mode, onstop method :
@override protected void onstop() { super.onstop(); if (!isfinishing()) { // don't hang around. finish(); } }
this same method in android alarmclock source code. if alter :
@override protected void onstop() { super.onstop(); if (!isfinishing()) { // don't hang around. //finish(); } }
it's working....
what happens looking why onstop beeing called. appears android when running activity sleep mode, creates set onstop. then, finish() destroy it.
i still don't undersant why android wants activity go onstop (and why it's not bug in orignal source code of alarmclock), it's working , i'm happy.
thanks seek help me.
android wakelock keyguard
Comments
Post a Comment