How to create an alarm wake screen in android and disable default back button? -
hello every 1 request please give me solution of question . have created alarm wake screen when alarm started has 2 buttons snooze , dismiss , work fine . problem started when user press button on phone disappears screen , sound still playing. if have solution please provide me. thank you
this alarmservice class
import android.app.service; import android.content.intent; import android.os.ibinder; public class alarmservice extends service { public static string tag = alarmservice.class.getsimplename(); @override public ibinder onbind(intent intent) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { intent alarmintent = new intent(getbasecontext(), alarmscreen.class); alarmintent.addflags(intent.flag_activity_new_task); try{ alarmintent.putextras(intent); getapplication().startactivity(alarmintent); alarmmanagerhelper.setalarms(this); }catch(exception e) { e.printstacktrace(); system.out.println(" may null intent found if not snooz or dismiss button user on alarm screen"); } return super.onstartcommand(intent, flags, startid); } }
and 1 receiver class create alarm screen
public class alarmscreen extends activity{ public final string tag = this.getclass().getsimplename(); private wakelock mwakelock; private mediaplayer mplayer; public static final string id = "id"; public static final string name = "name"; public static final string time_hour = "timehour"; public static final string time_minute = "timeminute"; public static final string tone = "alarmtone"; public static final string note = "note"; public static final string user = "user"; static string name; static string note; static string username; static string tone; static int timehour; static int timeminute; private static final int wakelock_timeout = 60 * 1000; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setup layout this.setcontentview(r.layout.activity_alarm_screen); /*parentview.getbackground().setalpha(128);*/ name = getintent().getstringextra(alarmmanagerhelper.name); note = getintent().getstringextra(alarmmanagerhelper.note); username = getintent().getstringextra(alarmmanagerhelper.user); timehour = getintent().getintextra(alarmmanagerhelper.time_hour, 0); timeminute = getintent().getintextra(alarmmanagerhelper.time_minute, 0); tone = getintent().getstringextra(alarmmanagerhelper.tone); textview tvname = (textview) findviewbyid(r.id.alarm_screen_name); textview tvnote = (textview) findviewbyid(r.id.alarm_screen_note); textview tvuser = (textview) findviewbyid(r.id.txt_nameuser); tvname.settext(name); tvnote.settext(note); tvuser.settext(username); textview tvtime = (textview) findviewbyid(r.id.alarm_screen_time); tvtime.settext(string.format("%02d : %02d", timehour, timeminute)); displaymetrics metrics = getresources().getdisplaymetrics(); final int screenwidth = (int) (metrics.widthpixels * 0.50); final int screenheight = (int) (metrics.heightpixels * 0.50); // instance of vibrator current context final vibrator v = (vibrator) getsystemservice(context.vibrator_service); // start without delay // each element alternates between vibrate, sleep, vibrate, sleep... long[] pattern = {0,800,200,1000,300,1000,200,4000}; if (v.hasvibrator()) { log.v("can vibrate", "yes"); } else { log.v("can vibrate", "no"); } button dismissbutton = (button) findviewbyid(r.id.alarm_screen_button); dismissbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { mplayer.stop(); v.cancel(); finish(); } }); //adding snooz button button btn_snooz = (button) findviewbyid(r.id.btn_snooz); btn_snooz.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { mplayer.stop(); v.cancel(); sooz_activity(); system.out.println("snooz activity started"); finish(); } }); //play alarm tone mplayer = new mediaplayer(); try { if (tone != null && !tone.equals("")) { uri toneuri = uri.parse(tone); if (toneuri != null) { mplayer.setdatasource(this, toneuri); mplayer.setaudiostreamtype(audiomanager.stream_alarm); mplayer.setlooping(true); mplayer.prepare(); mplayer.start(); v.vibrate(pattern, 0); } } } catch (exception e) { e.printstacktrace(); } //ensure wakelock release runnable releasewakelock = new runnable() { @override public void run() { getwindow().clearflags(windowmanager.layoutparams.flag_turn_screen_on); getwindow().clearflags(windowmanager.layoutparams.flag_keep_screen_on); getwindow().clearflags(windowmanager.layoutparams.flag_show_when_locked); getwindow().clearflags(windowmanager.layoutparams.flag_dismiss_keyguard); // getwindow().setlayout(screenwidth, screenheight); /* getwindow().setflags(windowmanager.layoutparams.wrap_content, windowmanager.layoutparams.wrap_content);*/ if (mwakelock != null && mwakelock.isheld()) { mwakelock.release(); } } }; new handler().postdelayed(releasewakelock, wakelock_timeout); } @suppresswarnings("deprecation") @override protected void onresume() { super.onresume(); // set window keep screen on getwindow().addflags(windowmanager.layoutparams.flag_turn_screen_on); getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); getwindow().addflags(windowmanager.layoutparams.flag_show_when_locked); getwindow().addflags(windowmanager.layoutparams.flag_dismiss_keyguard); // getwindow().setlayout(screenwidth, screenheight); // acquire wakelock powermanager pm = (powermanager) getapplicationcontext().getsystemservice(context.power_service); if (mwakelock == null) { mwakelock = pm.newwakelock((powermanager.full_wake_lock | powermanager.screen_bright_wake_lock | powermanager.acquire_causes_wakeup), tag); } if (!mwakelock.isheld()) { mwakelock.acquire(); log.i(tag, "wakelock aquired!!"); } } @override protected void onpause() { super.onpause(); if (mwakelock != null && mwakelock.isheld()) { mwakelock.release(); } } public void sooz_activity(){ pendingintent pintent = creatependingintent(this); long currenttimemillis = system.currenttimemillis(); long nextupdatetimemillis = currenttimemillis + 5 * dateutils.minute_in_millis; time nextupdatetime = new time(); nextupdatetime.set(nextupdatetimemillis); setalarm(this, nextupdatetimemillis, pintent); } private static pendingintent creatependingintent(context context) { intent intent = new intent(context, alarmservice.class); //intent.putextra(id, model.id); intent.putextra(name,name); intent.putextra(time_hour,timehour); intent.putextra(time_minute,timeminute); intent.putextra(tone,tone); intent.putextra(note,note); intent.putextra(user,username); return pendingintent.getservice(context,1, intent, pendingintent.flag_update_current); } @suppresslint("newapi") private static void setalarm(context context, long nextupdatetimemillis, pendingintent pintent) { alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); if (android.os.build.version.sdk_int >= android.os.build.version_codes.kitkat) { alarmmanager.setexact(alarmmanager.rtc_wakeup, nextupdatetimemillis, pintent); } else { alarmmanager.set(alarmmanager.rtc_wakeup, nextupdatetimemillis, pintent); } } }
and works fine when alarm screen appears on phone main screen , user pressed default phone button disappear main screen , sound , vibration continue until go stop running task.
i think "the new boston" has great video understanding going on here. https://www.youtube.com/watch?v=-g91hp3t6sg might looking onpause() stop sound. think video starting point monitoring how app behaving in different states.
Comments
Post a Comment