This tutorial 可能对未来的搜索者有用。
要完成这项工作,首先需要创建一个继承自 BroadcastReceiver 并实现 onReceive() 方法的类。这个方法告诉BroadcastReceiver 在检测到正确的 Intent 时应该做什么。因此,目前,正在定义 BroadcastReceiver 类的子类,以便在触发 BroadcastReceiver 时必须执行的代码。在本教程的后面,您将了解如何过滤意图以检测方向变化。为简单起见,下面的代码检测设备的方向变化和角度,并在 Toast 通知中显示此信息。代码如下:
package fortyonepost.com.orientationbrec;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.Toast;
public class OrientationBroadcastReceiver extends BroadcastReceiver
{
//An integer that holds the value of the orientation given by the current configuration
private int configOrientation;
//A WindowManager object that will act as a handle to the window service
private WindowManager wm;
//An integer that holds the value of the orientation (in degrees) given by the window service
private int windowServOrientation;
@Override
public void onReceive(Context context, Intent intent)
{
//Get the orientation from the current configuration object
configOrientation = context.getResources().getConfiguration().orientation;
//Display the current orientation using a Toast notification
switch (configOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
{
Toast.makeText(context, "Orientation is LANDSCAPE", Toast.LENGTH_SHORT).show();
break;
}
case Configuration.ORIENTATION_PORTRAIT:
{
Toast.makeText(context, "Orientation is PORTRAIT", Toast.LENGTH_SHORT).show();
break;
}
case Configuration.ORIENTATION_SQUARE:
{
Toast.makeText(context, "Orientation is SQUARE", Toast.LENGTH_SHORT).show();
break;
}
case Configuration.ORIENTATION_UNDEFINED:
default:
{
Toast.makeText(context, "Orientation is UNDEFINED", Toast.LENGTH_SHORT).show();
break;
}
}
//Get a handle to the Window Service
wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
//Query the current orientation made available by the Window Service
//The getOrientation() method is deprecated. Instead, use getRotation() when targeting Android API 8 (Android 2.2 - Froyo) or above.
windowServOrientation = wm.getDefaultDisplay().getOrientation();
//Display the current orientation using a Toast notification
switch (windowServOrientation)
{
case Surface.ROTATION_0:
{
Toast.makeText(context, "Rotation is 0 degrees.", Toast.LENGTH_SHORT).show();
break;
}
case Surface.ROTATION_90:
{
Toast.makeText(context, "Rotation is 90 degrees.", Toast.LENGTH_SHORT).show();
break;
}
case Surface.ROTATION_180:
{
Toast.makeText(context, "Rotation is 180 degrees.", Toast.LENGTH_SHORT).show();
break;
}
case Surface.ROTATION_270:
{
Toast.makeText(context, "Rotation is 270 degrees.", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
这是负责注册的 Activity 和 unregistering BroadcastReceiver。代码如下:
package fortyonepost.com.orientationbrec;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class OrientationBroadcastReceiverActivity extends Activity
{
//Create and initialize an OrientationBroadcastReceiver object
private OrientationBroadcastReceiver orientationBR = new OrientationBroadcastReceiver();
//Create and initialize a new IntentFilter
private IntentFilter orientationIF = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume()
{
//Register the Orientation BroadcasReceiver
this.registerReceiver(orientationBR, orientationIF);
super.onResume();
}
@Override
protected void onPause()
{
//Unregister the Orientation BroadcasReceiver to avoid a BroadcastReceiver leak
this.unregisterReceiver(orientationBR);
super.onPause();
}
}