2010-08-18 37 views

Trả lời

5

SensorManager.getOrientation(float[] R, float[] values) là cuộc gọi API chuẩn để sử dụng kể từ mức API 3.

+0

Yeah, tôi phát hiện ra rằng trong tài liệu. Hai câu hỏi nữa: bạn khởi tạo R và các giá trị thành gì? Ngoài ra, có cách nào để nhận thông báo hay toàn bộ mô hình làm việc không được chấp nhận? –

+1

OK, tìm thấy một ví dụ tốt trong cơ sở nguồn. Tôi sẽ không sao chép nó ở đây, nhưng nếu bạn duyệt kho git cho Android, hãy xem phần cuối của phát triển/mẫu/Compass/src/com/example/android/la bàn/CompassActivity.java –

+1

Thông báo/sự kiện là một cách không tốt để làm các cảm biến, đó là lý do tại sao nó được deprectated afaik. Mọi thay đổi khía cạnh nhỏ sẽ kích hoạt vô số sự kiện, về cơ bản nghẹn luồng giao diện người dùng với dữ liệu. – CodeFusionMobile

8

Sau đây là một ví dụ cơ bản mà lấy tiêu đề la bàn và hiển thị nó trong một TextView. Nó làm như vậy bằng cách thực hiện giao diện SensorEventListener. Bạn có thể thay đổi tốc độ các sự kiện được gửi đến hệ thống bằng cách thay đổi hằng số trong dòng mã sau (ví dụ: "mSensorManager.registerListener (this, mCompass, SensorManager.SENSOR_DELAY_NORMAL);") (xem sự kiện OnResume()); tuy nhiên, cài đặt chỉ là gợi ý cho hệ thống. Ví dụ này cũng sử dụng các phương thức onReuse() và onPause() để bảo toàn tuổi thọ pin bằng cách đăng ký và hủy đăng ký Trình nghe khi không sử dụng. Hi vọng điêu nay co ich.

package edu.uw.android.thorm.wayfinder; 

import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class CSensorActivity extends Activity implements SensorEventListener { 
private SensorManager mSensorManager; 
private Sensor mCompass; 
private TextView mTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layoutsensor); 
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
    mCompass = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 
    mTextView = (TextView) findViewById(R.id.tvSensor); 
} 

// The following method is required by the SensorEventListener interface; 
public void onAccuracyChanged(Sensor sensor, int accuracy) {  
} 

// The following method is required by the SensorEventListener interface; 
// Hook this event to process updates; 
public void onSensorChanged(SensorEvent event) { 
    float azimuth = Math.round(event.values[0]); 
    // The other values provided are: 
    // float pitch = event.values[1]; 
    // float roll = event.values[2]; 
    mTextView.setText("Azimuth: " + Float.toString(azimuth)); 
} 

@Override 
protected void onPause() { 
    // Unregister the listener on the onPause() event to preserve battery life; 
    super.onPause(); 
    mSensorManager.unregisterListener(this); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    mSensorManager.registerListener(this, mCompass, SensorManager.SENSOR_DELAY_NORMAL); 
} 
} 

Sau đây là các tập tin XML liên quan:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvSensor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 
+3

Đây có phải là vẫn không sử dụng cảm biến Orientation không được chấp nhận mà OP được đề cập đến không? – Tim

+0

Vâng - không được chấp nhận. – Vaiden

Các vấn đề liên quan