[英]how to check if widget is in background in android
我正在開發火炬。 我已經開發了一個按鈕的小部件。 如果單擊該按鈕,它將打開手電筒;如果再次單擊,則將關閉手電筒。 但是,當割炬打開且用戶按下電源按鈕以關閉屏幕時,它也會關閉割炬。 因此,我想檢查在割炬打開時設備是否已進入睡眠模式,有人可以幫我嗎?
這是我在小部件中打開和關閉手電筒的代碼
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);
}
從PowerManager系統服務檢查isScreenOn()
方法
您的應用或小部件是否可以保存並在以后使用savedInstanceState()
檢查其狀態,如有關恢復活動的android文檔http://developer.android.com/training/basics/activity-lifecycle/recreating.html中所述 ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.