android - Phone doesnt get silent on entering Geofence -


whenever enter geofence want turn phone silent.the notification should appear too. however,on entering geofence gets silent 1 second, time while notification generated, after notification displayed on screen,it turns normal mode automatically. please me out on how permanently turn phone silent on entering geofence , turn normal on leaving geofence.here geofenceintentservice class->

public class geofenceintentservice extends intentservice {     private final string tag = this.getclass().getcanonicalname();      /**      * creates intentservice.  invoked subclass's constructor.      *      * //@param name used name worker thread, important debugging.      */     private audiomanager audiomanager;     public geofenceintentservice() {         super("geofenceintentservice");     }     public void oncreate() {         super.oncreate();        log.v(tag, "oncreate geofenceintentservice");     }      public void ondestroy() {         super.ondestroy();         log.v(tag, "ondestroy");     }      @override     protected void onhandleintent(intent intent) {         geofencingevent geofencingevent = geofencingevent.fromintent(intent);         log.v(tag, "onhandleintent");         audiomanager = (audiomanager)getsystemservice(context.audio_service);         if(!geofencingevent.haserror()) {             int transition = geofencingevent.getgeofencetransition();             string notificationtitle;             if(transition==1 || transition==4)             {                 audiomanager=(audiomanager)getsystemservice(audio_service);                 audiomanager.setstreamvolume(audiomanager.stream_music,6,0);                 audiomanager.setringermode(audiomanager.ringer_mode_vibrate | audiomanager.ringer_mode_silent);             }             else if(transition==2)             {                 audiomanager.setringermode(audiomanager.ringer_mode_normal);              }             switch(transition) {                 case geofence.geofence_transition_enter: //                    int flag = packagemanager.component_enabled_state_enabled; //                    componentname cname = new componentname(geofenceintentservice.this,ringermodechangereceiver.class); //                    audiomanager.setringermode(audiomanager.ringer_mode_vibrate | audiomanager.ringer_mode_silent); //                    getpackagemanager().setcomponentenabledsetting(cname,flag,packagemanager.dont_kill_app);                     log.i("the phone is:", getsystemservice(context.audio_service) + "");                     notificationtitle = "geofence entered";                     log.v(tag, "geofence entered");                     break;                 case geofence.geofence_transition_dwell:                     notificationtitle = "geofence dwell";                     log.v(tag, "dwelling in geofence");                     break;                 case geofence.geofence_transition_exit:                      notificationtitle = "geofence exit";                     log.v(tag, "geofence exited");                     break;                 default:                     notificationtitle = "geofence unknown";             }             log.i("notification", "sent");             sendnotification(this, gettriggeringgeofences(intent), notificationtitle);         }         else         {              log.i("geofence ","error");         }     }     private void sendnotification(context context, string notificationtext,                                   string notificationtitle) {          powermanager pm = (powermanager) context                 .getsystemservice(context.power_service);         powermanager.wakelock wakelock = pm.newwakelock(                 powermanager.partial_wake_lock, "");         wakelock.acquire();          notificationcompat.builder notificationbuilder = new notificationcompat.builder(                 context).setsmallicon(r.drawable.index)                 .setcontenttitle(notificationtitle)                 .setcontenttext(notificationtext)                 .setdefaults(notification.default_all).setautocancel(false);           notificationmanager notificationmanager = (notificationmanager) context                 .getsystemservice(context.notification_service);         notificationmanager.notify(0, notificationbuilder.build());         audiomanager audiomanage = (audiomanager) getsystemservice(context.audio_service);         audiomanage.setringermode(audiomanager.ringer_mode_silent);         toast.maketext(getapplicationcontext(), "now in silent  mode", toast.length_short).show();         wakelock.release();          audiomanage.setringermode(audiomanager.ringer_mode_normal);     }     private string gettriggeringgeofences(intent intent) {         geofencingevent geofenceevent = geofencingevent.fromintent(intent);         log.i("event is:",geofenceevent+"");         list<geofence> geofences = geofenceevent                 .gettriggeringgeofences();            return "mobile turned silent";     }  } 

in code in method sendnotification() have

audiomanage.setringermode(audiomanager.ringer_mode_silent); toast.maketext(getapplicationcontext(), "now in silent  mode", toast.length_short).show(); wakelock.release();  audiomanage.setringermode(audiomanager.ringer_mode_normal); 

so set mode silent , straight normal within few lines of code, hence why solution not work want to.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -