2012-09-02 35 views
0

我想在設備的方向更改上創建新活動,而不更改當前的活動方向。有沒有辦法做到這一點?在Android中檢查設備方向

附加信息:

我總應用默認的方向是Portrate。當設備處於橫向時,我必須打開第二個活動。但第一項活動應在Portrate 。

回答

0

謝謝大家的幫助。最後,我通過使用SensorEventListener來完成此操作。

的OnCreate()

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 

監聽

private SensorEventListener mySensorEventListener = new SensorEventListener() { 

    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 

    public void onSensorChanged(SensorEvent event) { 
     // angle between the magnetic north directio 
     // 0=North, 90=East, 180=South, 270=West 
     float azimuth = event.values[1]; 
     // compassView.updateData(azimuth); 

     if ((azimuth < 100 && azimuth > 60) 
       || (azimuth > -100 && azimuth < -60)) { 
      if (EasterVal != 0) { 
       if (!Modules.LoanType.equalsIgnoreCase("Ballcash")) { 
        Intent in = new Intent(Buyeroutput.this, Esteregg.class); 
        startActivity(in); 
        EasterVal = 0; 
       } 
      } 
     } else { 
      EasterVal = 1; 
     } 
    } 
}; 

protected void onDestroy() { 
    super.onDestroy(); 
    if (sensor != null) { 
     sensorManager.unregisterListener(mySensorEventListener); 
    } 
} 
2

您可以在您的AndroidMenifest.xml中使用android:screenOrienttation="Landscape/Portrate"進行相關活動。因此,其餘活動將保留在默認視圖中,並且強化活動將強制在您的維度中顯示在特定視圖中。

希望這對你有所幫助。

+0

設置默認方位AndroidMenifest.xml的活動結束後,我無法識別該活動的屏幕方向。 – Sniper

+0

無需重新定位方向,因爲您的審稿人活動處於橫向或端口模式,因爲您在清單文件中有內容。 – Akshay

+0

看到你所要做的只是使用'screenOrientiation =「Landscape」'屬性爲你想在landscape.Ex中進行的活動。 ''就像你想要的風景那樣。 – Akshay

2

當然你可以。您只需在清單中聲明您的第一個活動將處理方向更改。然後添加一個onConfigurationChanged函數來開始你的第二個活動。

+0

我用onConfigurationChanged。但是我必須檢查方向而不改變第一個活動的方向。 – Sniper

1

寫在Menifest

的第一個活動

<activity android:name="firstActivity" android:screenOrientation="portrait"></activity> 

爲第二活動

<activity android:name="secondActivity" android:screenOrientation="landscape"></activity> 
+0

一旦我將第一個活動(它必須在方向更改時打開第二個活動)設置爲portrate。 getResources()。getConfiguration()。orientation不識別方向更改。 – Sniper

+0

呵呵k我們也可以設置運行時setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); –

1

您也可以以編程方式檢測到您當前的活動方向和運行的意圖到您的第二個活動,只要您的設備處於橫向模式。這裏是一個代碼來完成它:

WindowManager wm = getWindowManager(); 
Display d = wm.getDefaultDisplay(); 
if(d.getWidth() > d.getHeight()) 
{ 
    // landscape mode 
    Intent i = new Intent(FirstActivity.this, SecondActivity.class); 
    startActivity(i); 
} 
+0

可以請指導在哪裏調用這些功能。我想在我改變方向時調用它。 – Sniper

+0

在onCreate()方法 –

+0

因爲我在AndroidMenifest.xml中設置了android:screenOrientation =「portrait」。 onCreate只會被調用一次。我希望你的功能在景觀中被調用。不改變活動方向。 – Sniper