警报不会唤醒设备

时间:2014-07-29 14:29:35

标签: android alarmmanager alarm android-alarms wakeup

我已经为Android创建了一个祈祷时间应用程序,其中包括athan时间的警报,但有些时间没有在适当的时间唤醒设备它将会延迟

这是我的警报意图

public class AlarmScreenActivity extends Activity {

public final String TAG = this.getClass().getSimpleName();

private WakeLock mWakeLock;
private MediaPlayer mPlayer;
private static final int WAKELOCK_TIMEOUT = 60 * 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Setup layout
    this.setContentView(R.layout.activity_alarm_screen);

    // Set the window to keep screen on
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    // Acquire wakelock
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    if (mWakeLock == null) {
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , TAG);
    }

    if (!mWakeLock.isHeld()) {
        mWakeLock.acquire();
        Log.i(TAG, "Wakelock aquired!!");
    }


    Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
    dismissButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mPlayer.stop();
            finish();
        }
    });
    dismissButton.setTypeface(face);
    dismissButton.setText(res.getString(R.string.rd_ok));
    //Play alarm tone
    mPlayer = new MediaPlayer();
    try {
        Uri toneUri = Uri.parse(tone);
        mPlayer.setDataSource(this, toneUri);
        mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mPlayer.prepare();
        mPlayer.start();


    } catch (Exception e) {
        e.printStackTrace();
    }

    //Ensure wakelock release
    Runnable releaseWakelock = new Runnable() {

        @Override
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

            if (mWakeLock != null && mWakeLock.isHeld()) {
                mWakeLock.release();
            }
        }
    };

    new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}

@Override
protected void onPause() {
    super.onPause();

    if (mWakeLock != null && mWakeLock.isHeld()) {
        mWakeLock.release();
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mPlayer.isPlaying()) {
        mPlayer.stop();
    }
}

}

我添加了androidmanifest文件中的权限

<uses-permission android:name="android.permission.WAKE_LOCK" />

请帮我纠正

1 个答案:

答案 0 :(得分:2)

为什么不尝试

  

getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

按此线程的建议 Difference between Wakelock and FLAG_KEEP_SCREEN_ON?

如果是报警则更有意义。