I’m trying to make a simple alarm trigger. When alarm goes off there should a a simple sound alarm. But the alarm doesn’t go off, nothing happens. Now I’m not sure where am I mistaken – is it the times? Intents? Receivers?
Class in which I’m using AlarmManager
package com.example.prva;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class Meni_Splash extends Activity{
Calendar c = Calendar.getInstance();
private int hour = c.get(Calendar.HOUR_OF_DAY);
private int minute = c.get(Calendar.MINUTE);
int milivreme;
int milipicker;
long sveskupa;
int hoursad;
int minutesad;
protected boolean timeactive = true;
Handler h=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h.post(new Runnable(){
@Override
public void run() {
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
TextView text = (TextView) findViewById(R.id.textDate);
text.setText(currentDateTimeString);
h.postDelayed(this, 1000);
}
});
final TimePicker picker = (TimePicker) findViewById(R.id.timePicker);
final TextView text = (TextView) findViewById(R.id.texttime);
picker.setIs24HourView(true);
picker.setCurrentHour(hour);
picker.setCurrentMinute(minute);
Button btnt = (Button) findViewById(R.id.buttontime);
btnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
hoursad = picker.getCurrentHour();
minutesad = picker.getCurrentMinute();
milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
text.setText(new StringBuilder().append(hoursad).append(":").append(minutesad).append( "Trenutno vrijeme u milisekundama : ").append(System.currentTimeMillis()).append(" vrijeme izabrano u milisekundama sve skupa ").append(milipicker));
}
});
Button btnalarm = (Button) findViewById(R.id.btnalarm);
btnalarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hoursad = picker.getCurrentHour();
minutesad = picker.getCurrentMinute();
milivreme = (((hour)* 60 * 60 * 1000)+ ((minute) * 60 * 1000));
milipicker = (((hoursad) * 60 * 60 * 1000) + ((minutesad) * 60 * 1000));
sveskupa = milipicker - milivreme;
Intent intent = new Intent(Meni_Splash.this, AlarmReceiver.class);
PendingIntent pendingintent = PendingIntent.getActivity(Meni_Splash.this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + sveskupa, pendingintent);
}
});
Button btnv = (Button) findViewById(R.id.buttonv);
btnv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Meni_Splash.this, button.class));
}
});
}
}
AlarmReceiver class :
package com.example.prva;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class AlarmReceiver extends Activity {
protected MediaPlayer MPAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MPAlarm = MediaPlayer.create(this, R.raw.splash);
MPAlarm.start();
}
};
Look like there problem in AlarmManager set() function.
AlarmManager Set() =>
public void set (int type, long triggerAtMillis, PendingIntent operation)
Parameters
type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC or RTC_WAKEUP.
triggerAtMillis time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).
operation Action to perform when the alarm goes off; typically comes from IntentSender.getBroadcast().
Please note that operation (PendingIntent), will get the action from IntentSender.getBroadcast().
Hope it’s helps.