не срабатывает будильник при заблокированном экране
#39375203
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Добрый день, прошу вашей помощи в проблеме- будильник не срабатывает при заблокированном экране.
В манифесте разрешения даны
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<activity android:name=".alarmclock.AlarmAlert" android:label="@string/alert_title"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Dialog"
android:launchMode="singleTask"
android:showOnLockScreen="true"
android:taskAffinity=":AlarmAlert"
android:screenOrientation="sensorPortrait"
android:configChanges="orientation|keyboardHidden|keyboard|navigation" />
Код активити
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382. 383. 384. 385. 386. 387. 388. 389. 390. 391. 392. 393. 394. 395. 396. 397. 398. 399. 400. 401. 402. 403. 404. 405. 406. 407. 408. 409. 410. 411. 412. 413. 414.
import android.app.Activity;
import android.app.Dialog;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.widget.TextView;
import android.os.PowerManager;
import android.content.pm.ActivityInfo;
import java.util.Calendar;
import com.gufa.myalarm2.R;
public class AlarmAlert extends Activity implements Alarms.AlarmSettings {
private static final int SNOOZE_MINUTES = 10;
private static final int UNKNOWN = 0;
private static final int SNOOZE = 1;
private static final int DISMISS = 2;
private static final int KILLED = 3;
private KeyguardManager mKeyguardManager;
private KeyguardManager.KeyguardLock mKeyguardLock;
private Button mSnoozeButton;
private int mState = UNKNOWN;
private AlarmKlaxon mKlaxon;
private int mAlarmId;
private AlarmPreference mAlarmPref;
private String mLabel;
private ShakeListener mShakeListener;
private int mSnooze;
private int mDuration;
private int mCaptchaSnooze, mCaptchaDismiss;
private boolean mCaptchaShown = false;
private boolean mCaptchaDone = false;
private PowerManager.WakeLock mWakeLock;
public static final String DEBUG_TAG = AlarmReceiver.class.getSimpleName();
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
AlarmAlertWakeLock.acquire(this);
Log.v("AlarmAlert.onCreate()");
setWindowSettings();
releaseWakeLock();
acquireWakeLock();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
lp.token = null;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
Intent i = getIntent();
mAlarmId = i.getIntExtra(Alarms.ID, -1);
Alarms.getAlarm(getContentResolver(), this, mAlarmId);
mKlaxon = new AlarmKlaxon();
mKlaxon.postPlay(this, mAlarmId);
setTitleFromIntent(i);
Alarms.disableSnoozeAlert(AlarmAlert.this);
Alarms.disableAlert(AlarmAlert.this, mAlarmId);
Alarms.setNextAlert(this);
mKlaxon.setKillerCallback(new AlarmKlaxon.KillerCallback() {
public void onKilled() {
if (Log.LOGV) Log.v("onKilled()");
updateSilencedText();
/* don't allow snooze */
mSnoozeButton.setEnabled(false);
dismiss();
mState = KILLED;
}
});
updateLayout();
SharedPreferences settings = getSharedPreferences(AlarmClock.PREFERENCES, 0);
if (settings.getBoolean(AlarmClock.PREF_SHAKE_SNOOZE, true)) {
mShakeListener = new ShakeListener(this);
mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
snooze();
if (mCaptchaSnooze == 0)
finish();
}
});
}
}
private void setTitleFromIntent(Intent i) {
mLabel = i.getStringExtra(Alarms.LABEL);
if (mLabel == null || mLabel.length() == 0) {
mLabel = getString(R.string.default_label);
}
setTitle(mLabel);
}
private void setWindowSettings()
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}}
public void acquireWakeLock()
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE
| PowerManager.PARTIAL_WAKE_LOCK, "alarming_wakelock");
mWakeLock.acquire();
}
public void releaseWakeLock()
{
if (mWakeLock != null)
{
if (mWakeLock.isHeld())
{
mWakeLock.release();
mWakeLock = null;
}
}
}
private void updateSilencedText() {
TextView silenced = (TextView) findViewById(R.id.silencedText);
silenced.setText(getString(R.string.alarm_alert_alert_silenced,
mDuration / 1000*60));
silenced.setVisibility(View.VISIBLE);
}
private void updateLayout() {
setContentView(R.layout.alarm_alert);
AlarmAlertWakeLock.acquire(this);
LayoutInflater mFactory = LayoutInflater.from(this);
SharedPreferences settings =
getSharedPreferences(AlarmClock.PREFERENCES, 0);
int face = settings.getInt(AlarmClock.PREF_CLOCK_FACE, 0);
if (face < 0 || face >= AlarmClock.CLOCKS.length) {
face = 0;
}
View clockLayout =
(View) mFactory.inflate(AlarmClock.CLOCKS[face], null);
ViewGroup clockView = (ViewGroup) findViewById(R.id.clockView);
clockView.addView(clockLayout);
if (clockLayout instanceof DigitalClock) {
((DigitalClock) clockLayout).setAnimate();
}
mSnoozeButton = (Button) findViewById(R.id.snooze);
if (mSnooze > 0) {
mSnoozeButton.requestFocus();
mSnoozeButton.setVisibility(View.VISIBLE);
}
else { // hide the snooze button if snooze disabled for this alarm
mSnoozeButton.setVisibility(View.GONE);
}
if (mState == KILLED) {
updateSilencedText();
mSnoozeButton.setEnabled(false);
} else {
mSnoozeButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
snooze();
if (mCaptchaSnooze == 0)
finish();
}
});
}
findViewById(R.id.dismiss).setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
dismiss();
if (mCaptchaDismiss == 0)
finish();
}
});
}
private void snooze() {
if (mState != UNKNOWN) {
return;
}
if (mSnooze == 0) { // snooze disabled for this alarm
return;
}
if (mCaptchaSnooze != 0 && !mCaptchaDone) {
show_captcha(mCaptchaSnooze, SNOOZE);
return;
}
final long snoozeTime = System.currentTimeMillis()
+ (1000 * 60 * mSnooze);
final long nextAlarm =
Alarms.calculateNextAlert(AlarmAlert.this).getAlert();
String displayTime = null;
if (nextAlarm < snoozeTime) {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(nextAlarm);
displayTime = getString(R.string.alarm_alert_snooze_not_set,
Alarms.formatTime(AlarmAlert.this, c));
mState = DISMISS;
} else {
Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId, snoozeTime,
mLabel);
Alarms.setNextAlert(AlarmAlert.this);
displayTime = getString(R.string.alarm_alert_snooze_set,
mSnooze);
mState = SNOOZE;
}
Log.v(displayTime);
Toast.makeText(AlarmAlert.this, displayTime, Toast.LENGTH_LONG).show();
mKlaxon.stop(this, mState == SNOOZE);
releaseLocks();
finish();
}
private void dismiss() {
if (mState != UNKNOWN) {
return;
}
if (mCaptchaDismiss != 0 && !mCaptchaDone) {
show_captcha(mCaptchaDismiss, DISMISS);
return;
}
mState = DISMISS;
mKlaxon.stop(this, false);
releaseLocks();
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
AlarmAlertWakeLock.acquire(this);
if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
mState = UNKNOWN;
mSnoozeButton.setEnabled(true);
disableKeyguard();
mAlarmId = intent.getIntExtra(Alarms.ID, -1);
mKlaxon.postPlay(this, mAlarmId);
setTitleFromIntent(intent);
TextView silenced = (TextView)findViewById(R.id.silencedText);
silenced.setVisibility(View.GONE);
Alarms.setNextAlert(this);
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
if (Log.LOGV) Log.v("AlarmAlert.onResume()");
disableKeyguard();
if (mShakeListener != null)
mShakeListener.resume();
}
@Override
protected void onStop() {
super.onStop();
AlarmAlertWakeLock.acquire(this);
if (Log.LOGV) Log.v("AlarmAlert.onStop()");
snooze();
releaseLocks();
if (mShakeListener != null)
mShakeListener.pause();
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
updateLayout();
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean up = event.getAction() == KeyEvent.ACTION_UP;
boolean dismiss = false;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENDCALL:
break;
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
dismiss = true;
default:
if (mState == UNKNOWN && up) {
if (dismiss) {
dismiss();
} else {
snooze();
}
}
return true;
}
return super.dispatchKeyEvent(event);
}
private synchronized void enableKeyguard() {
if (mKeyguardLock != null) {
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
}
private synchronized void disableKeyguard() {
if (mKeyguardLock == null) {
mKeyguardLock = mKeyguardManager.newKeyguardLock(Log.LOGTAG);
mKeyguardLock.disableKeyguard();
}
}
private synchronized void releaseLocks() {
AlarmAlertWakeLock.release();
enableKeyguard();
}
public void reportAlarm(
int idx, boolean enabled, int hour, int minutes,
Alarms.DaysOfWeek daysOfWeek, boolean vibrate, String message, String alert,
int snooze, int duration, int delay, boolean vibrate_only,
int volume, int crescendo,
int captcha_snooze, int captcha_dismiss
) {
mSnooze = snooze;
mCaptchaSnooze = captcha_snooze;
mCaptchaDismiss = captcha_dismiss;
mDuration = duration;
updateLayout();
}
private void show_captcha(int captcha_type, int which_state)
{
CaptchaInterface captcha;
if (captcha_type == Alarms.CAPTCHA_TYPE_PUZZLE) {
captcha = new PuzzleCaptcha(this);
}
else
if (captcha_type == Alarms.CAPTCHA_TYPE_MATH) {
captcha = new MathCaptcha(this);
}
else
return;
if (mCaptchaShown) return;
mCaptchaShown = true;
final int which = which_state;
captcha.setOnCorrectListener(new CaptchaInterface.OnCorrectListener() {
public void onCorrect() {
mCaptchaDone = true;
if (which == SNOOZE) {
snooze();
}
else
if (which == DISMISS) {
dismiss();
}
finish();
}
});
((Dialog)captcha).setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
mCaptchaShown = false;
}
});
captcha.show();
}
}
люди добрые, помогите разобраться что не так в коде
|
|