123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
|
package com.AndroidOrientation; import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast; public class AndroidOrientation extends Activity{ TextView orientation; MyOrientationEventListener myOrientationEventListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); orientation = (TextView)findViewById(R.id.orientation); myOrientationEventListener = new MyOrientationEventListener(
this , SensorManager.SENSOR_DELAY_NORMAL); if (myOrientationEventListener.canDetectOrientation()){
myOrientationEventListener.enable(); } else { Toast.makeText(AndroidOrientation. this , "Can't Detect Orientation!" , Toast.LENGTH_LONG).show(); } } @Override protected void onDestroy() { super .onDestroy(); myOrientationEventListener.disable(); } class MyOrientationEventListener
extends OrientationEventListener{ public MyOrientationEventListener(Context context,
int rate) {
super (context, rate); } @Override public void onOrientationChanged( int arg0) {
orientation.setText(String.valueOf(arg0)); } } } |