I've written the following code to fire an alarm when the clock changes to the next minute (say it's 8:32 and 32 seconds, it'll fire at 8:33:00).. and then repeat every hour. Here's my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends Activity {
private AlarmManager mAlarmManager;
private Intent popup, popup_logger;
private PendingIntent pending_popup;
private final long dHour = 3600000;
private final long dTime = 1000*60;
private static final String TAG = "TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Begin Alarm Setup
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
popup = new Intent(MainActivity.this,TimeRecorder.class);
pending_popup = PendingIntent.getBroadcast(
MainActivity.this,0,popup,0);
long timeNow = System.currentTimeMillis();
long nextMinute = (timeNow % dTime) + timeNow;
mAlarmManager.setRepeating(1,nextMinute,dHour,pending_popup);
Date date = new Date(nextMinute);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String dateFormatted = formatter.format(date);
Log.i(TAG,"Alarm time: " + dateFormatted);
//End Alarm Setup
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
The Log.i outputs a time in the future when the alarm should be firing. But it never fires (it never opens my "TimeRecorder" activity.)
Any ideas what's happening? I've only learned Android programming for about a week and only know basic Java and i've tried reading several solutions but they all deal with code more complicated than mine. Any ideas?