Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1157518
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: 嵌入式

2010-08-04 08:58:26

ActivityMain.java
 
 

package com.yuanlin.turnOverSilent;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.TextView;

public class ActivityMain extends Activity {
    private TextView mTextView;
    private SensorManager mSensorManager;
    private SensorEventListener mSensorEventListener = new SensorEventListener() {

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            switch (event.sensor.getType()) {
            case SensorManager.SENSOR_ORIENTATION:
                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                if (event.values[2] < -10) {
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                } else {
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                }
                mTextView.setText("" + String.valueOf(event.values[2]));
                break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextView = (TextView) findViewById(R.id.textViewId);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        mSensorManager.registerListener(mSensorEventListener, mSensorManager
                .getDefaultSensor(SensorManager.SENSOR_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
    }

    @Override
    protected void onStop() {
        mSensorManager.unregisterListener(mSensorEventListener);
        super.onStop();
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/background">
    <TextView android:id="@+id/textViewId" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"
        android:textSize="20dip" android:textStyle="bold" android:textColor="#00FF00" />
</LinearLayout>


阅读(848) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~