我正在开发火炬。我用一个按钮开发了小部件。如果单击该按钮,它将打开手电筒,如果再次单击,它将关闭手电筒。但是当手电筒打开并且用户按下电源按钮关闭屏幕时,它也会关闭手电筒。所以我想检查手电筒打开时设备是否进入睡眠模式,有人可以帮我吗?
这是我在小部件中打开和关闭手电筒的代码
public void onReceive(Context context, Intent intent) {
Log.d("torchWidget", "onReceive");
final String action = intent.getAction();
if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if(appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else{
if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String msg = "null";
try{
msg = intent.getStringExtra("msg");
msg = "Flash Turned On";
} catch(NullPointerException e) {
Log.e("Error", "msg = null");
}
if(Constants.isFlashOnWidget){
Constants.isFlashOnWidget = false;
Toast.makeText(context, "Flash turned OFF", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context.getApplicationContext(),
BackgroundService.class);
context.stopService(serviceIntent);
} else {
Constants.isFlashOnWidget = true;
Intent serviceIntent = new Intent(context.getApplicationContext(),
BackgroundService.class);
context.startService(serviceIntent);
}
}
super.onReceive(context, intent);
}