3

此問題涉及3D幾何體和對Android傳感器的深入理解。我花了幾周的時間尋找解決方案,但沒有成功,並會很感謝社區的幫助。Android:從服務獲取當前設備方向

是否有可能從Android中的服務獲取當前設備方向(縱向或橫向)?我不需要傳感器的連續更新;只是當前的設備方向。即使啓動器是縱向的(因爲用戶已將自動旋轉設置爲關閉),設備方向仍應在橫向持有時正確報告。

示例/示例代碼將受到高度讚賞。

謝謝。

+1

發現任何解決呢? – zaxy78

+0

您設法找到解決方案? – Denis

回答

2

是的,你可以

WindowManager windowService = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
int currentRatation = windowService.getDefaultDisplay().getRotation(); 

if (Surface.ROTATION_0 == currentRatation) { 
    currentRatation = PORT; 
} else if(Surface.ROTATION_180 == currentRatation) { 
    currentRatation = PORT; 
} else if(Surface.ROTATION_90 == currentRatation) { 
    currentRatation = LAND; 
} else if(Surface.ROTATION_270 == currentRatation) { 
    currentRatation = LAND; 
} 
+1

當主屏幕上方的設備方向更改時,此功能無效。僅當設備屏幕方向更改時,此api才能正常工作。 – Meher