I am working on a application that would create an alarm on the screen. Every hour when I press a button. I have one class where I define the AlarmManager, WakeLock and A window that will open on fullscreen when the alarm is triggered, and I am launching it from my MainActivity.class. But I have no error and the alarm will simply not run. I am a newbie so please point me in a right direction. I have done my research but I simply cannot find what is wrong with my code.
MyAlarmReceiverActivity code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.alarm);
Button stopAlarm = (Button) findViewById(R.id.btnStopAlarm);
stopAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
mMediaPlayer.stop();
finish();
}
});
playSound(this, getAlarmUri());
}
private void playSound(Context context, Uri alert)
{
mMediaPlayer = new MediaPlayer();
try
{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!=0)
{
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}catch (IOException e)
{
Log.i("AlarmReceiver","No audio files are found!");
}
}
private Uri getAlarmUri(){
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
protected void onStop()
{
super.onStop();
mWakeLock.release();
}
On my main Activity i am running it with this code
public void spusteniePripomienok (){
Toast.makeText(this, "Spustil si pripomienky každých "+ interval, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, AlarmRecieverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT );
AlarmManager am = (AlarmManager) getSystemService(MainActivity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
}
Can you try changing your PowerManager code to something like this to see if it works:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "");
mWakeLock.acquire();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.