If you think the Android project android-class listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2007 The Android Open Source Project
*/*www.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.android.deskclock;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
/**
* Full screen alarm alert: pops visible indicator and plays alarm tone. This
* activity shows the alert as a dialog.
*/publicclass AlarmAlert extends AlarmAlertFullScreen {
// If we try to check the keyguard more than 5 times, just launch the full
// screen activity.
privateint mKeyguardRetryCount;
privatefinalint MAX_KEYGUARD_CHECKS = 5;
privatefinal Handler mHandler = new Handler() {
@Override
publicvoid handleMessage(Message msg) {
handleScreenOff((KeyguardManager) msg.obj);
}
};
privatefinal BroadcastReceiver mScreenOffReceiver =
new BroadcastReceiver() {
@Override
publicvoid onReceive(Context context, Intent intent) {
KeyguardManager km =
(KeyguardManager) context.getSystemService(
Context.KEYGUARD_SERVICE);
handleScreenOff(km);
}
};
@Override
protectedvoid onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Disable custom title, this will already be shown as a dialog */// findViewById(R.id.topPanel).setVisibility(View.GONE);
// Listen for the screen turning off so that when the screen comes back
// on, the user does not need to unlock the phone to dismiss the alarm.
registerReceiver(mScreenOffReceiver,
new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
@Override
publicvoid onDestroy() {
super.onDestroy();
unregisterReceiver(mScreenOffReceiver);
// Remove any of the keyguard messages just in case
mHandler.removeMessages(0);
}
@Override
publicvoid onBackPressed() {
finish();
}
@Override
protectedint getLayoutResId() {
return R.layout.alarm_alert;
}
privateboolean checkRetryCount() {
if (mKeyguardRetryCount++ >= MAX_KEYGUARD_CHECKS) {
Log.e("Tried to read keyguard status too many times, bailing...");
return false;
}
return true;
}
privatevoid handleScreenOff(final KeyguardManager km) {
if (!km.inKeyguardRestrictedInputMode() && checkRetryCount()) {
if (checkRetryCount()) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(0, km), 500);
}
} else {
// Launch the full screen activity but do not turn the screen on.
Intent i = new Intent(this, AlarmAlertFullScreen.class);
i.putExtra(Alarms.ALARM_INTENT_EXTRA, mAlarm);
i.putExtra(SCREEN_OFF, true);
startActivity(i);
finish();
}
}
}