azrosen92
azrosen92

Reputation: 9057

Broadcast Receiver not going off in android app

I am using the following code to set my android app to silence the phone at the time given by event.getStartTime()

public static void schedule(Context ctx, Event event) {
    //schedule method is being called, but the alarm manager is not being set for some reason
    Calendar start = event.getStartTime();
    long end = event.getEndTime().getTimeInMillis();
    String status = event.getStatus();

    Intent intent = new Intent(ctx, AlarmReceiver.class);
    intent.putExtra("time", end);
    intent.putExtra("status", status);

    PendingIntent sender = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), sender);
}

And here is my onReceive method in my AlarmReceiver class which extends BroadcastReceiver

@Override
    public void onReceive(Context context, Intent intent) {
        //Toast.makeText(context, "changing status", Toast.LENGTH_SHORT).show();
        ContextWrapper c = new ContextWrapper(context);
        AudioManager am = (AudioManager) c.getBaseContext().getSystemService(Context.AUDIO_SERVICE);

        Calendar set_time = Calendar.getInstance();
        int current_ringer_mode = am.getRingerMode();
        String current_status = "";

        switch (current_ringer_mode) {
            case AudioManager.RINGER_MODE_VIBRATE: current_status = "vibrate";
            break;

            case AudioManager.RINGER_MODE_SILENT: current_status = "silence";
            break;

            case AudioManager.RINGER_MODE_NORMAL: current_status = "sound";
            break;

            default: current_status = "silence";
            break;
        }

        try {
            Bundle bundle = intent.getExtras();
            String status = bundle.getString("status");
            set_time.setTimeInMillis(bundle.getLong("time"));
            if (status.equals("vibrate")) {
                //turn phone on vibrate
                am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                if (bundle.getLong("time") != 0L) {
                    //set unscheduler
                    EventScheduler.unschedule(context.getApplicationContext(), 
                            set_time, 
                            current_status);
                }
            } else if (status.equals("silence")) {
                //turn phone on silence
                am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                if (bundle.getLong("time") != 0L) {
                    //set unscheduler
                    EventScheduler.unschedule(context.getApplicationContext(), 
                            set_time, 
                            current_status);
                }
            } else if (status.equals("sound")) {
                //turn on sound
                am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            }

        } catch(Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

However when the time comes around when this event is supposed to go off, nothing happens. Am I doing this correctly?

Upvotes: 0

Views: 294

Answers (1)

Rajeev
Rajeev

Reputation: 1404

Silly doubt, but did you register the BroadcastReceiver and the required IntentFilter?

EDIT: In your manifest file you should have the below lines inside the application node

<receiver android:name="your_receiver_name" >
    <intent-filter>
        <action android:name="your_action_name" >
        </action>
    </intent-filter>
</receiver>

The other way to this dynamically is, call registerReceiver (BroadcastReceiver receiver, IntentFilter filter)

For a nice tutorial on how to use Broadcast Filters you can visit this Vogella Tutorial

Hope this will clear your doubt and resolve the issue.

Upvotes: 1

Related Questions