cyberbemon
cyberbemon

Reputation: 3200

Android Alarm Manager not triggering event

I'm trying to set an alarm for 3 specific times and they'll be repeated everyday.

I have 2 Arraylists, one holds the pendingIntents and the other holds the Calendar times.

ArrayList<PendingIntent>intentarray = new ArrayList<PendingIntent>();
ArrayList<Calendar>calTimes = new ArrayList<Calendar>();

I've the following times set up

    Calendar cal = Calendar.getInstance();
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();

    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY,9);
    cal.set(Calendar.MINUTE,00);
    calTimes.add(cal);

    cal1.setTimeInMillis(System.currentTimeMillis());
    cal1.set(Calendar.HOUR_OF_DAY,15);
    cal1.set(Calendar.MINUTE,00);
    calTimes.add(cal1);

    cal2.setTimeInMillis(System.currentTimeMillis());
    cal2.set(Calendar.HOUR_OF_DAY,21);
    cal2.set(Calendar.MINUTE,00);
    calTimes.add(cal2);

Followed by an Array to hold AlarmManagers

AlarmManager[] alarmManager=new AlarmManager[3];

Then in a loop I assign each of the alarms to the alarmmanager.

for(int i =0; i < calTimes.size(); i++){

        Intent alarmIntent = new Intent(this,AlarmReceiver.class);
        pendingIntent = pendingIntent.getBroadcast(this,i,alarmIntent,0);

        alarmManager[i] = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

        intentarray.add(pendingIntent);
    }

`

When I run the app and change the times to trigger the alarm. Nothing happens.

This is my BroadcastReceiver.

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"starting App",Toast.LENGTH_LONG).show();
        startApp(context,"com.example.myapp");
    }

    public void startApp(Context context, String packageName){
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}

Upvotes: 0

Views: 1322

Answers (3)

Anderson K
Anderson K

Reputation: 5505

This works:

AlarmManager alarmManager;


alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


 private void alarms(){

        ArrayList<Integer> intentarray = new ArrayList<>();
        ArrayList<Calendar> calTimes = new ArrayList<>();

        Calendar cal = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, 9);
        cal.set(Calendar.MINUTE, 00);
        calTimes.add(cal);

        cal1.setTimeInMillis(System.currentTimeMillis());
        cal1.set(Calendar.HOUR_OF_DAY, 15);
        cal1.set(Calendar.MINUTE, 00);
        calTimes.add(cal1);

        cal2.setTimeInMillis(System.currentTimeMillis());
        cal2.set(Calendar.HOUR_OF_DAY, 21);
        cal2.set(Calendar.MINUTE,00);
        calTimes.add(cal2);


        Intent alarmIntent = new Intent(this, AlarmReceiver.class);

        for(int i = 0; i < calTimes.size(); i++){

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, alarmIntent,0);

            //Wake up the device to fire the alarm at approximately
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
            intentarray.add(i);
        }
    }

Edited:

if you need to awaken your Activity without using startActivity u need something like this:

<uses-permission android:name="android.permission.WAKE_LOCK"/>


import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class AlarmReceiver extends WakefulBroadcastReceiver {

    public static PowerManager.WakeLock wakeLock;

    public AlarmReceiver() {
    }

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

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
        wakeLock.acquire();

        Toast.makeText(context, "starting App", Toast.LENGTH_LONG).show();
        startApp(context,"com.example.myapp");

        wakeLock.release();
    }

    public void startApp(Context context, String packageName){

        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);

        if (intent != null){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}

Upvotes: 0

issathink
issathink

Reputation: 1230

If you want to set many alarms, you have to give different id to your intents (otherwise the new alarm replaces the previous alarm).

for(int i =0; i < calTimes.size(); i++){

    Intent alarmIntent = new Intent(this,AlarmReceiver.class);
    intent.setData(Uri.parse("timer:" + i));
    pendingIntent = pendingIntent.getBroadcast(this,i,alarmIntent,0);

    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

    intentarray.add(pendingIntent);
}

Upvotes: 0

Yash Sampat
Yash Sampat

Reputation: 30611

You are adding an Intent to the AlarmManager. You need to add a PendingIntent.

Replace

alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

with

alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Another thing: you don't need to use an array of AlarmManager objects to add repeating alarms. A single instance of AlarmManager will do.

Upvotes: 1

Related Questions