开发者社区> 问答> 正文

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

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

展开
收起
爵霸 2016-03-20 08:55:16 5100 0
1 条回答
写回答
取消 提交回答
  • 屏幕旋转开关:
    1). 屏幕旋转开关设置主要调用android.provider.Settings.SystemputIntgetInt方法实现。
    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);  
        }  
    }  
    
    
    } 

    权限添加:

    2019-07-17 19:08:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载