Android:从广播接收器获取屏幕旋转
我有一个报警管理器,每5分钟执行一次意图广播:Android:从广播接收器获取屏幕旋转,android,broadcastreceiver,alarmmanager,screen-orientation,android-broadcast,Android,Broadcastreceiver,Alarmmanager,Screen Orientation,Android Broadcast,我有一个报警管理器,每5分钟执行一次意图广播: AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 300000, PendingIntent.getBroadcast(context, 0, new Intent(
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));
在BroadcastReceive的onReceive方法中,我需要知道设备方向(0°、90°、180°、270°):
一切正常。但当我最小化或关闭应用程序时,屏幕的旋转总是返回0°(android主屏幕的方向),即使设备水平放置
我如何从广播接收器知道设备的旋转,而不受应用程序或设备状态的影响?可能是您正在搜索的内容
context.getResources().getConfiguration().orientation
您可以获得实时更改。可能就是您正在搜索的内容
context.getResources().getConfiguration().orientation
您可以获得实时更改。我终于使用加速计解决了这个问题:
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);
}
我最后用加速度计解决了这个问题:
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);
}
可能对未来的搜索者有用
要完成这项工作,首先需要创建一个类,该类继承自BroadcastReceiver
,并实现onReceive()
方法。此方法说明当检测到正确的意图时,BroadcastReceiver
应该做什么。因此,目前正在定义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;
}
}
}
}
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();
}
}
下面是负责注册和取消注册广播接收器的活动。代码如下:
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;
}
}
}
}
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();
}
}
可能对未来的搜索者有用
要完成这项工作,首先需要创建一个类,该类继承自BroadcastReceiver
,并实现onReceive()
方法。此方法说明当检测到正确的意图时,BroadcastReceiver
应该做什么。因此,目前正在定义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;
}
}
}
}
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();
}
}
下面是负责注册和取消注册广播接收器的活动。代码如下:
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;
}
}
}
}
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();
}
}
谢谢@M66B,但它返回“横向”或“纵向”。我需要知道旋转(0°、90°、180°或270°)谢谢@M66B,但它返回“横向”或“纵向”。我需要知道旋转角度(0°、90°、180°或270°)