我是Android \ Java的新手,我有以下问题: 如何检查屏幕方向更改?
当然我发现了很多关于这个主题的文章,但是没有一篇文章适合我,所以我可能做错了。
我不明白的一件事是: 我需要实现OrientationEventListener吗? 或者是" onConfigurationChanged"够了? 或者我需要两者吗?
例如,我尝试运行使用" onConfigurationChanged"的下列代码。 - 但是永远不会调用onConfigurationChanged。 (我已将android:configChanges =" orientation | screenSize | keyboardHidden"添加到manifest.xml,我为API构建> 13)
所以我的问题是: 为什么" onConfigurationChanged()"从未执行过? 我已经在真实设备(三星Galaxy Note)和模拟器上尝试过它。
public class main_activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
@Override
public void onConfigurationChanged(Configuration newConfig) <--- this is never called
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test.andro" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".main_activity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
的build.gradle:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.test.andro"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
答案 0 :(得分:1)
试试这个
orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
public void onOrientationChanged(int orientation) {
if(canShow(orientation)){
show();
} else if(canDismiss(orientation)){
dismiss();
}
}
};
@Override
public void onResume(){
super.onResume();
orientationListener.enable();
}
@Override
public void onPause(){
super.onPause();
orientationListener.disable();
}
private boolean isLandscape(int orientation){
return orientation >= (90 - THRESHOLD) && orientation <= (90 + THRESHOLD);
}
private boolean isPortrait(int orientation){
return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD);
}
public boolean canShow(int orientation){
return !visible && isLandscape(orientation);
}
public boolean canDismiss(int orientation){
return visible && !dismissing && isPortrait(orientation);
}
答案 1 :(得分:0)
在您想要检查活动方向的地方使用此代码:
protected void onResume(Bundle savedInstanceState)
{
super.onResume();
int rotation = getWindowManager().getDefaultDisplay().getRotation();
// DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(dm);
if(rotation == 0)
text="Portrait";
else if(rotation == 1)
text="Rotated left side";
else
text="Rotated right side";
Toast toast = Toast.makeText(getApplicationContext(), text,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER | Gravity.CENTER, 10, 0);
toast.show();
}