3

I want to make a service which fire alarm manager in every 1 min interval..But my Alarm run once(first time only). I follow Lalit Answer

private class Receiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            Toast.makeText(getBaseContext(), "Alarm", Toast.LENGTH_LONG).show();
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
            AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent i=new Intent(context, ConnectionReceiver.class);
            PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
            mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1*60*1000, pi);
        }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mercy
  • 1,862
  • 9
  • 39
  • 75
  • wants to discuss with you, please come back in chat room – Lucifer Jan 25 '12 at 07:24
  • Have you tried [Commonsware Example](https://github.com/commonsguy/cw-advandroid/blob/master/SystemServices/Alarm/src/com/commonsware/android/syssvc/alarm/OnBootReceiver.java) Try this. – Praveenkumar Jan 25 '12 at 07:41
  • ya i tried Installing screen.apk... [2012-01-25 13:03:30 - screen] Success! [2012-01-25 13:03:30 - screen] \screen\bin\screen.apk installed on device [2012-01-25 13:03:30 - screen] Done! – Mercy Jan 25 '12 at 07:45

3 Answers3

3

Juts register broadcast receiver for:

http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • again it comes first time only – Mercy Jan 25 '12 at 07:42
  • This action is fired every minute by system. Just register broadcast receiver for it in your manifest. No need to set up alarm manager at all – Konstantin Pribluda Jan 25 '12 at 08:09
  • from android doc: "You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver()." – rmanna Jul 04 '13 at 07:30
2

Try this code in Your broadcast receiver's onReceive method

AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                              60000+System.currentTimeMillis(),
                                  getPendingIntent(ctxt));

and you can get pending intent

private static PendingIntent getPendingIntent(Context ctxt) {
    Intent i=new Intent(ctxt, AReceiver.class);
    return(PendingIntent.getBroadcast(ctxt, 0, i, 0));
}

where AReceiver class is for start service like Notification it is working fine in my app so i hope it helps you

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
Ajay
  • 4,850
  • 2
  • 32
  • 44
0

I know this question already has an answer, but for others who have had the same issue but need to use AlarmManager. The reason why it only runs once is because the new PendingIntent you create does not get recreated, but rather is reusing the one before it. So in other words, the reason why your alarm only ran once was because it kept reusing it. Using the flags to refresh the intent extras if there are any should be doing the trick, but that also does not work.

A trick to use to make sure it does not reuse the PendingIntent and ultimately the Intent you provide is to use setAction() and give it some unique "Action". I did it like this:

intent.setAction("com.yourname."+System.currentTimeMillis());

As you see this makes sure its unique. Though, the above accepted answer it the best approach, if someone does not want that, they need to understand why and how to remedy that issue. Hope it helps anyone else.

Andy
  • 10,553
  • 21
  • 75
  • 125