android - Trying to lock screen rotation when displaying dialogs -


i trying lock screen in whatever orientation user in when application launches dialog , unlocking when dialog dismisses. here lock , unlock code:

   // sets screen rotation fixed current rotation setting      private void mlockscreenrotation() {          log.d("####################", "screen orientation " + mcontext.getresources().getconfiguration().orientation);          // stop screen orientation changing during event          if (mcontext.getresources().getconfiguration().orientation == 1)               ((activity) mcontext).setrequestedorientation(activityinfo.screen_orientation_portrait);                  else ((activity) mcontext).setrequestedorientation(activityinfo.screen_orientation_landscape); //       switch (mcontext.getresources().getconfiguration().orientation) //       { //          case configuration.orientation_portrait: //              ((activity) mcontext).setrequestedorientation( //                  activityinfo.screen_orientation_portrait); //              break; //          case configuration.orientation_landscape: //              ((activity) mcontext).setrequestedorientation( //                  activityinfo.screen_orientation_landscape); //              break; //       }      }       // allow screen rotations again      private void munlockscreenrotation()  {          this.setrequestedorientation(                  activityinfo.screen_orientation_unspecified);          misscreenrotationlocked = false;      } 

i call mlockscreenrotation() when launch dialogs , call munlockscreenrotation() in handler , in dialoginterface.onclicklistener's.

sometimes screen remains locked, not consistent. suggestions or ideas why or how handle this?

thanks in advance help!

ammendment: code locking screen:

 public void displayprogressdialog(){         mlockscreenrotation();         // local handle on class progress dialog optimization purposes         progressdialog temp = mprogressdialog = new progressdialog(this);          // message string (for reason dialog can't handle res ids messages)         string message = getstring(r.string.downloading);          // set paramaters         temp.setindeterminate(true);         temp.settitle(r.string.weather_data);         temp.setmessage(message);         temp.setcancelable(false);         temp.getwindow().setflags(windowmanager.layoutparams.flag_blur_behind,                  windowmanager.layoutparams.memory_type_push_buffers /*windowmanager.layoutparams.flag_blur_behind*/);           temp.show();     }      public void displaylocationsearchdialog(){         mlockscreenrotation();         // local handle on class progress dialog optimization purposes         progressdialog temp = mcoordinatesearchdialog = new progressdialog(this);          // message string (for reason dialog can't handle res ids messages)         string message = getstring(r.string.searching);          // set paramaters         temp.setindeterminate(true);         temp.settitle(r.string.location_search);         temp.setmessage(message);         temp.setcancelable(false);         temp.getwindow().setflags(windowmanager.layoutparams.flag_blur_behind,                  windowmanager.layoutparams.flag_blur_behind);          temp.show();      }      public void showdatafeedfaileddialog(){         mlockscreenrotation();         new alertdialog.builder(this)             .settitle(r.string.network_error)             .setmessage(r.string.weather_data_failed)             .setpositivebutton(r.string.try_again, mtryagainlistener)             .setnegativebutton(r.string.dismiss, null)             .create()             .show();     }      public void showcoordinatesearchfaileddialog(){         mlockscreenrotation();         new alertdialog.builder(this)         .settitle(r.string.network_error)         .setmessage(r.string.location_search_failed)         .setpositivebutton(r.string.try_again, mcoordtryagainlistener)         .setnegativebutton(r.string.dismiss, null)         .create()         .show();     }      private void showgpsalertdialog(){         mlockscreenrotation();         new alertdialog.builder(this)             .settitle(r.string.gps_error)             .setmessage(r.string.gps_error_details)             .setneutralbutton(r.string.dismiss, null)             .setpositivebutton(r.string.go_to_settings, mtosettingslistener)             .create()             .show();     }      private void showgpssearchingdialog(){         mlockscreenrotation();         progressdialog temp = mgpssearchalertdialog = new progressdialog(this);          string message = getstring(r.string.location_services_details);         string btntext = getstring(r.string.cancel);          temp.setindeterminate(true);         temp.settitle(r.string.location_services);         temp.setmessage(message);         temp.setbutton(btntext, mcancelgpssearchlistener);         temp.setcancelable(true);         temp.getwindow().setflags(windowmanager.layoutparams.flag_blur_behind,                  windowmanager.layoutparams.flag_blur_behind);          temp.show();     }      private void showgpstimeoutalertdialog(){         mlockscreenrotation();         new alertdialog.builder(this)         .settitle(r.string.gps_error)         .setmessage(r.string.gps_timeout_message)         .setpositivebutton(r.string.try_again, mgpstimeoutlistener)         .setnegativebutton(r.string.dismiss, mgpstimeoutlistener)  // check line free if still no         .create()         .show();     }      private void showweatheralertdialog(){         log.d("############", "weather alert dialog");         mlockscreenrotation();         string message = null;         if(mwd.getwarningtypes() == null) return;         int cnt = 0;         int size = mwd.getwarningtypes().size() - 1;         for(string warningtype : mwd.getwarningtypes()){             if(cnt == 0) message = warningtype;             else if(cnt == size) message += " , " + warningtype;             else message += ", " + warningtype;             cnt++;         }          new alertdialog.builder(this)             .settitle(r.string.watches_and_warnings)             .setmessage(message)             .setpositivebutton(r.string.go_to_accuweather, mtoalertweblistener)             .setneutralbutton(r.string.dismiss, null)             .create()             .show();     }      private void showneedlocationalertdialog() {         mlockscreenrotation();         new alertdialog.builder(this).settitle(r.string.error).setmessage(         r.string.add_location).setneutralbutton(r.string.dismiss, null)         .setpositivebutton(r.string.okay, mtolocationsearchlistener)         .create().show();     }      private void showconnectivityalertdialog() {         mlockscreenrotation();         new alertdialog.builder(this).settitle(r.string.network_error)         .setmessage(r.string.no_connection).setneutralbutton(         r.string.dismiss, null).create().show();     }      private void showcurrenturlinbrowser(){         // show current conditions web page         if(mwd.geturl() == null || mwd.geturl().length() 

this bad solution, works. tested on lg gt540(android 2.3.7) , asus transformer(android 3.2):

private void stoprotate() {     if(getresources().getconfiguration().orientation == 1)     {         if( display.getorientation() == 1 || display.getorientation() == 2)             setrequestedorientation(9);         else                 setrequestedorientation(activityinfo.screen_orientation_portrait);     }     else     {         if( display.getorientation() == 2 || display.getorientation() == 3)             setrequestedorientation(8);         else                 setrequestedorientation(activityinfo.screen_orientation_landscape);     } }  private void startrotate() {     setrequestedorientation(activityinfo.screen_orientation_unspecified);     setrequestedorientation(activityinfo.screen_orientation_sensor); } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -