就是下图这里的自动旋转屏幕开关,开启和关闭自动旋转系统会发送广播吗?什么广播?怎么监听它的状态改变?
屏幕旋转开关:
1). 屏幕旋转开关设置主要调用android.provider.Settings.System
的putInt
和getInt
方法实现。
2). 通过ContentObserver
来动态观察屏幕旋转设置的改变。
示例代码如下:
package com.example.srs;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ScreenRotationSwitch extends Activity implements OnClickListener
{
private Button mRotationButton;
private RotationObserver mRotationObserver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建观察类对象
mRotationObserver = new RotationObserver(new Handler());
mRotationButton = (Button) findViewById(R.id.rotation);
refreshButton();
mRotationButton.setOnClickListener(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//解除观察变化
mRotationObserver.stopObserver();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//注册观察变化
mRotationObserver.startObserver();
}
//更新按钮状态
private void refreshButton()
{
if (getRotationStatus(this) == 1)
{
mRotationButton.setText(R.string.rotation_off);
}
else
{
mRotationButton.setText(R.string.rotation_on);
}
}
//得到屏幕旋转的状态
private int getRotationStatus(Context context)
{
int status = 0;
try
{
status = android.provider.Settings.System.getInt(context.getContentResolver(),
android.provider.Settings.System.ACCELEROMETER_ROTATION);
}
catch (SettingNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return status;
}
private void setRotationStatus(ContentResolver resolver, int status)
{
//得到uri
Uri uri = android.provider.Settings.System.getUriFor("accelerometer_rotation");
//沟通设置status的值改变屏幕旋转设置
android.provider.Settings.System.putInt(resolver, "accelerometer_rotation", status);
//通知改变
resolver.notifyChange(uri, null);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (getRotationStatus(this) == 1)
{
setRotationStatus(getContentResolver(), 0);
}
else
{
setRotationStatus(getContentResolver(), 1);
}
}
//观察屏幕旋转设置变化,类似于注册动态广播监听变化机制
private class RotationObserver extends ContentObserver
{
ContentResolver mResolver;
public RotationObserver(Handler handler)
{
super(handler);
mResolver = getContentResolver();
// TODO Auto-generated constructor stub
}
//屏幕旋转设置改变时调用
@Override
public void onChange(boolean selfChange)
{
// TODO Auto-generated method stub
super.onChange(selfChange);
//更新按钮状态
refreshButton();
Toast.makeText(ScreenRotationSwitch.this, "旋转屏幕设置有变化",
Toast.LENGTH_SHORT).show();
}
public void startObserver()
{
mResolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.ACCELEROMETER_ROTATION), false,
this);
}
public void stopObserver()
{
mResolver.unregisterContentObserver(this);
}
}
}
权限添加:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。