android中重写onConfigurationChanged方法响应系统设置更改

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

本文中利用按钮动态改变屏幕方向,然后重写Activity的onConfigurationChanged方法,该方法用于监听系统设置的更改,代码如下:

Activity:

package com.lovo;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获得改变系统布局的按钮
		Button btn = (Button) findViewById(R.id.btn1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 获得系统的Configuration对象
				Configuration config = getResources().getConfiguration();
				// 如果当前是横屏
				if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
					// 设为竖屏
					TestActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				}
				// 如果当前是竖屏
				if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
					// 设为横屏
					TestActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				}
			}
		});
	}

	// 重写该方法,用于监听系统设置的更改,主要是监控屏幕方向的更改
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横屏"
				: "竖屏";
		Toast.makeText(this, "系统屏幕方向发生改变" + "\n修改后的屏幕方向为:" + screen,
				Toast.LENGTH_LONG).show();
	}
}


布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更改屏幕方向" />

</LinearLayout>


更改系统设置时需要在manifest声明权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lovo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!-- android:configChanges="orientation":设置Activity允许修改屏幕方向 -->
        <activity
            android:configChanges="orientation"
            android:name=".TestActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          <activity
            android:name=".TestListViewActivity"
            android:label="@string/app_name" 
             android:theme="@style/AppTheme">  
        </activity>
    </application>
    <!-- 授予应用程序修改系统设置的权限 -->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
</manifest>

附上图片效果:


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值