我有一个 AlarmManager ,每隔5分钟执行一次意图广播:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 300000, PendingIntent.getBroadcast(context, 0, new Intent(context, Sensors.class), 0));
在 BroadcastReceiver的onReceive 方法中,我需要知道设备方向(0°,90°,180°,270°):
int orientation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
一切正常。但是当我最小化或关闭应用程序时,屏幕的旋转总是返回0°(android主屏幕的方向),即使设备水平放置。
我如何知道设备从BroadcastReceiver的轮换,与应用程序或设备的状态无关?
答案 0 :(得分:3)
我终于使用加速计来解决这个问题:
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
final SensorEventListener sensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
sensorManager.unregisterListener(this);
int orientation;
// Vertical
if (Math.abs(event.values[1]) > Math.abs(event.values[0]) && Math.abs(event.values[1]) > Math.abs(event.values[2]))
if (event.values[1] > 0)
orientation = 0; // Head Up
else
orientation = 180; // Head Down
// Horizontal
else if (Math.abs(event.values[0]) > Math.abs(event.values[1]) && Math.abs(event.values[0]) > Math.abs(event.values[2]))
if (event.values[0] > 0)
orientation = 90; // Left
else
orientation = 270; // Right
// Flat
else
orientation = 0;
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
}
答案 1 :(得分:1)
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;
}
}
}
}
以下是负责注册的活动和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();
}
}
答案 2 :(得分:0)