Yamen Nassif
Yamen Nassif

Reputation: 2476

AlarmManager is working every time the application starts

I have been searching 3 days for this. I tried every single solution out there in google and stackoverflow.com.

I have a MainActivity which starts a BroadcastReceiver like this.

Calendar calendar3 = Calendar.getInstance();

    calendar3.set(Calendar.HOUR_OF_DAY, 7);
    calendar3.set(Calendar.MINUTE, 34);
    calendar3.set(Calendar.SECOND, 00);
    Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent);

i tried alarmManager.setReapeating too with no luck.

Then the BroadcastReceiver starts a service which gets some info from db and put it in a notification or more like this:

Intent i = new Intent(context,MyService.class);
context.startService(i);

the service

public class MyService extends Service {
MyDB db;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("Yamen", "service Started");
    db = new MyDB(getApplicationContext());
    GregorianCalendar calendar = new GregorianCalendar();
    Date time = calendar.getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String timeString = simpleDateFormat.format(time);
    SharedPreferences mySharedPreferences = getApplicationContext().getSharedPreferences("myPref",Context.MODE_PRIVATE);
    boolean isNoti=mySharedPreferences.getBoolean("isNoti",true);
    if (isNoti) {
        ArrayList<AnniBean> annis =
                db.getAnni(timeString);
        Log.e("Yamen", "size is " + annis.size());
        for (AnniBean anni : annis) {
            new NotificationCreator(getApplicationContext(), "Anniversary Reminder", anni.getAnniName(), anni.getAnniDetails());
        }
    }
    Log.e("Yamen", "service finished");
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

notification appears everyday at 7:34 am like a charm. The problem is the BroadcastReceiver and the service are being called every time i open the application too.

please don't refer me to any link because I did visited a lot of links on this. Thanks in advice

Upvotes: 2

Views: 696

Answers (2)

zeeali
zeeali

Reputation: 1594

Try with following code:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 34);
cal.set(Calendar.SECOND, 0);

if(cal.before(Calendar.getInstance())){ // if it's in the past, increment
    cal.add(Calendar.DATE, 1);
}

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

This alarm will run every day at specified time.

Upvotes: 1

Omar Alshaker
Omar Alshaker

Reputation: 879

The problem is in the Broadcast receiver intent XML. Its receiving other broadcasts, you should filter by action like this: In Java:

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
intent.setAction("your.package.name.your.anything");

in XML:

<receiver android:name=".MyBroadcastReceiver"> 
<intent-filter> 
        <action android:name="your.package.name.your.anything" />  
</intent-filter> 
</receiver>

Upvotes: 0

Related Questions