Reputation: 426
I am developing torch. I have developed widget with a single button. If you click on that button it turns on torch and if you click again it turns off torch. BUT when torch is ON and user pressed Power button to turn off screen it also turns off the torch. So I want to check if device has gone to sleep mode while torch was on, can someone help me ?
here is my code to turn torch on and off in widget
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);
}
Upvotes: 0
Views: 192
Reputation: 734
Could your app or widget save, and later check, its state using savedInstanceState()
, as described ib the android docs on restoring activities http://developer.android.com/training/basics/activity-lifecycle/recreating.html ?
Upvotes: 0