0

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?

xjdeng
  • 49
  • 7
  • refer to http://stackoverflow.com/questions/6520403/how-to-set-alarm-in-android also upvote the question – Nitin Nov 10 '14 at 04:54

2 Answers2

0
Try this code


    Intent myIntent = new Intent(this, TimeRecorder.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      long nextMinute = (timeNow % dTime) + timeNow;
            alarmManager.set(AlarmManager.RTC, nextMinute, pendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    nextMinute,dHour, pendingIntent);
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
  • Unfortunately, that didn't work although I probably should've put "AlarmManager.RTC" instead of simply "1". I figured it out eventually: it seems the Alarms can only fire a BroadcastReceiver, not another Activity. – xjdeng Nov 16 '14 at 05:14
  • This Alarm code is not working on kitkat plz make sure that your app target level is 17 – Rohit Heera Nov 17 '14 at 04:44
0

Make sure that your timerecorder.class extends broadcastReciever, all the code that executes on the alarmManager must be placed there, or that class can start a new intent that has all the code executed when the alarm is activited, also don't forget to register your broadcastReciever in the manifest. BTW you are doing pretty well for 1st week :)

endlesschaos
  • 145
  • 1
  • 6