`
mickey_hou
  • 浏览: 249087 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

turning phone

阅读更多
package com.huawei.voice.activity;
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.os.Bundle;

import com.huawei.voice.R;
import com.huawei.voice.util.Parameters;
import com.huawei.voice.util.ToastDeal;

/**
*
* 手机翻转事件处理  待测试
*
* @author  mickey_hou
* @version  1.0, 2012-5-17
* @since  Tiantian
*/
public class TurningDealActivity extends Activity implements SensorEventListener
{
    private SensorManager sensorManager;
   
    private ToastDeal deal = new ToastDeal();
   
    private long mLastUpdateTime;
   
    //private float mLastX, mLastY, mLastZ; 
   
    private Context myContext;
   
    public int shakeThreshold = 1500;
   
    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.main);
        myContext = TurningDealActivity.this;
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
   
    @Override
    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(
                    Sensor.TYPE_ACCELEROMETER),//TYPE_ACCELEROMETER  传感器类型 :加速度
                    SensorManager.SENSOR_DELAY_NORMAL);
    }
   
    @Override
    protected void onStop()
    {
        sensorManager.unregisterListener(this);
        super.onStop();
    }
   
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
    }
   
    public void onSensorChanged(SensorEvent event)
    {
        long currentTime = System.currentTimeMillis();
        long diffTime = currentTime - mLastUpdateTime;
        if (diffTime < Parameters.UPDATE_INTERVAL)
        {
            return;
        }
        this.mLastUpdateTime = currentTime;
       
        /*
         * 旋转角度测试
         */
        float[] values = event.values;
        float ax = values[0];
        float ay = values[1];

        double g = Math.sqrt(ax * ax + ay * ay);
        double cos = ay / g;
        if (cos > 1) {
            cos = 1;
        } else if (cos < -1) {
            cos = -1;
        }
        double rad = Math.acos(cos);
        if (ax < 0) {
            rad = 2 * Math.PI - rad;
        }

        int uiRot = getWindowManager().getDefaultDisplay().getRotation();
        double uiRad = Math.PI / 2 * uiRot;
        rad -= uiRad;
       
        /**
         * 当加速度的差值大于指定的阈值,认为这是一个摇晃 
         */
        if (rad >= 90 || rad < -90)
        {
            deal.showToast(myContext, "旋转角度为:"+rad);
        }
    }
}
分享到:
评论

相关推荐

    My.New.iPhone:52.Simple.Projects.To.Get.You.Started(No.Starch.2009-6)

    Turning an iPhone on and off should be easy— and it is. However, the iPhone can be in one of four states: on, off, sleep, or airplane mode. If all of this sounds more confusing than just turn- ing ...

    英语问路指路专题PPT课件.pptx

    为了提供更精确的指引,可以加上具体的信息,比如 “Turn right at the second turning.” 表示在第二个路口右转。另外,还会用到 “It is on the right / left.” 来描述目的地在哪个方向。 理解地图和使用方位...

    Pragmatic.Programming.Google.Glass.2nd.Edition

    An Android device, such as a smart phone or tablet, is also helpful, but not necessary. Table of Contents Chapter 1. Wrapping Your Head Around Glass Part I The Mirror API Chapter 2. The Google App ...

    (同步复习精讲辅导)北京市2014-2015学年七年级英语下册 Unit 8 Is there a post office ne

    具体到本单元,一些重要的词汇有邮局(post office)、警察局(police station)、酒店(hotel)、餐厅(restaurant)、银行(bank)、医院(hospital)、公用电话(pay phone)、邻里(neighborhood)以及一系列...

    人教版高中英语必修四现在分词巩固练习(含答案).pdf

    22. "On receiving a phone call from his wife saying she had a fall," "saying"是现在分词,作定语修饰"call",表示电话内容。 23. "Seeing is believing." 这是一句谚语,意为眼见为实。 24. "It’s amazing ...

    高考英语一轮知识点专题训练:时态和语态新题赏析.pdf

    题一:Now that we ____ all the money, it’s no use turning on me and saying it’s all my fault. 此句中,"Now that"引导的条件状语从句通常与现在完成时态连用,表示过去的动作对现在的影响,所以正确答案是C....

    OpenGL+ES+.pdf

    - 第四章"Turning On the Lights"会介绍OpenGL ES中的光照模型和渲染技术,可能包括如何在3D场景中添加光源、材质、着色、环境光、漫反射和镜面反射等概念。 - 第五章"Textures"将着重于纹理映射技术,讲解如何将...

    Travel journal 语法学案(人教版必修1)(1).doc

    - **表示变化中的状态**:常见于动词如get, grow, become, turn, run, go, begin等,如"The leaves are turning red."(叶子正在变红)和"It's getting warmer and warmer."(天气越来越暖和)。 - **与always, ...

    ios手电筒Demo

    print("Error turning off torch: \(error.localizedDescription)") } } } ``` ```objective-c - (void)toggleTorch { AVCaptureDevice *device = self.captureDevice; AVCaptureDevice TorchMode; if (device...

    汉语新词新译系列-H.docx

    #### 回头率(huitoulv)— head-turning rate **定义与解释:** 回头率(huitoulv),直译为“回头查看的频率”,这个词语用来形容一个人或物品在公共场所能够引起他人注意的能力。通常用于形容人的外貌吸引力,...

    turnjs帮助文档

    - 修正了 `turning + turned` 事件顺序问题 - 修正了在错误位置添加页面的问题 #### 四、使用方法 - **HTML 结构**: - turn.js 需要在 DOM 中指定一个容器元素来承载所有页面。 - 可通过三种方式添加页面到翻...

    初中英语鲁教版七年级下册Unit4单元检测及答案.docx

    Turning (现在分词或动名词) - D. To turn (不定式) 正确答案是 **A. Turn**。这是一个祈使句,用于表示命令或请求,因此需要使用动词原形。 #### 2. 介词 in/on/at 的用法 - **句子结构**: “Our class will ...

    完美版资料动词ing形式大全小学阶段.docx

    - **turn** → **turning** - **touch** → **touching** - **row** → **rowing** - **stand** → **standing** - **open** → **opening** - **wait** → **waiting** - **shout** → **shouting** - **laugh** →...

    Yunus, Muhammad - Creating A World Without Poverty; Social Business and the Future of Capitalism

    This volume efficiently recounts the story of microcredit, then discusses Social Business, organizations designed to help people while turning profits. French food giant Danone's partnership to ...

    iPod+Authentication+Coprocessor+Spec+2.0C+R1

    Starting Up the cp by turning power on /13 Starting Up the Cp by Warm Reset 14 Communication Process15/ Low-Power Sleep Mode-16 Chapter 4 Coprocessor Registers 17 Register Addresses 17 Register ...

Global site tag (gtag.js) - Google Analytics