2011-02-09 122 views
1

我試圖鎖定我的屏幕,無論用戶在應用程序何時啓動對話框,然後在對話框關閉時將其解鎖。這是我的鎖定和解鎖代碼:試圖在顯示對話框時鎖定屏幕旋轉

// Sets screen rotation as fixed to current rotation setting 
    private void mLockScreenRotation() { 
     Log.d("####################", "screen orientation is " + mContext.getResources().getConfiguration().orientation); 
     // Stop the screen orientation changing during an 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; 
    }

我打電話mLockScreenRotation()當我啓動任何對話,我呼籲mUnlockScreenRotation()在我的處理程序,並在我的DialogInterface.OnClickListener的。

有時我的屏幕保持鎖定狀態,但它不一致。任何建議或想法爲什麼或如何處理?

在此先感謝您的幫助!

Ammendment:代碼中我鎖我的屏幕:

 public void displayProgressDialog(){ 
     mLockScreenRotation(); 
     // Get local handle on class progress dialog for optimization purposes 
     ProgressDialog temp = mProgressDialog = new ProgressDialog(this); 

     // Get message string (for some reason this dialog can't handle res IDs for messages) 
     String message = getString(R.string.downloading); 

     // Set some 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(); 
     // Get local handle on class progress dialog for optimization purposes 
     ProgressDialog temp = mCoordinateSearchDialog = new ProgressDialog(this); 

     // Get message string (for some reason this dialog can't handle res IDs for messages) 
     String message = getString(R.string.searching); 

     // Set some 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 this line with free if still no good 
     .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 += " and " + 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()
+0

發佈代碼,您正在調用'mLockScreenRotation'。 – user432209

+0

我已將代碼添加到了我要求的mLockScreenRotation中。 – taraloca

回答

0

而不是把呼叫轉移到對話框中改變方向(然後可能駁回所述對話剛結束),嘗試推行onDismiss監聽到你的對話如概述here

+0

我需要在啓動對話框時鎖定初始屏幕方向的方向。我沒有看到這將如何用onDismiss監聽器鎖定我的屏幕。 – taraloca

+0

我可能誤解了這個問題。當你說「有時我的屏幕保持鎖定狀態」時,我認爲你的意思是即使在對話被解散之後(因爲屏幕在不應該出現之後仍然鎖定)。這是否是相反的,問題是它沒有鎖定始終在第一位? –

+0

嘗試重現,但它對我有用。您正在測試的設備的平臺版本是什麼?基於您的對話類型,行爲是否改變(始終鎖定,始終如此) '再現? –

1

這是一個不好的解決方案,但它的工作原理。在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); 
} 
相關問題