就是下圖這裡的自動旋轉屏幕開關,開啟和關閉自動旋轉系統會發送廣播嗎?什麼廣播?怎麼監聽它的狀態改變?
屏幕旋轉開關:
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);
}
}
}
權限添加: