baidu_23478311
梅明子
2015-12-29 02:18
采纳率: 91.9%
浏览 2.9k

Android 自动旋转屏幕快捷开关,系统会发送广播吗?

就是下图这里的自动旋转屏幕开关,开启和关闭自动旋转系统会发送广播吗?什么广播?怎么监听它的状态改变?
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • zby_2015
    zby_2015 2015-12-29 03:02
    已采纳

    屏幕旋转开关:
    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);  
        }  
    }  
    

    }

     权限添加:
    

    点赞 评论

相关推荐