Search code examples
androidbroadcastreceiver

Apparently my Intent does not get delivered


I have an application that needs to start and stop activities.

So far we are OK with starting the Activity.

The problem comes when I try to stop the Activity.

This is the AlarmManager that broadcasts the intent to close the activity:

        Intent ftue = new Intent(ctxt, VideoActivty.class);
        ftue.putExtra("finish", true);
        PendingIntent pftue = PendingIntent.getBroadcast(ctxt, 0, ftue, 0);
        Calendar calSet4 = Calendar.getInstance();
        calSet4.set(Calendar.MONTH, c.get(Calendar.MONTH));
        calSet4.set(Calendar.YEAR, c.get(Calendar.YEAR));
        calSet4.set(Calendar.DAY_OF_WEEK, 3);
        calSet4.set(Calendar.HOUR_OF_DAY, hftue);
        calSet4.set(Calendar.MINUTE, mftue);
        calSet4.set(Calendar.SECOND, 0);
        calSet4.set(Calendar.MILLISECOND, 0);

        //calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
        mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet4.getTimeInMillis(),
                7 * 24 * 60 * 60 * 1000, pftue);

And in my Activty I have implemented a BroadcastReceiver that should shut down the Activty.

But without success...

@Override
public void onResume() {
super.onResume();
IntentFilter f=new IntentFilter();
registerReceiver(receiver, f);
}


@Override
public void onPause() {
unregisterReceiver(receiver);
super.onPause();
}

BroadcastReceiver receiver=new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Log.e("","intento ricevuto");
        if(intent.getBooleanExtra("finish",false))finish();

    }
};

Solution

  • Let me make my comment an answer and expound on it.

    If you use an empty IntentFilter, your activity won't ever receive any Intents broadcasted to it. However, if your broadcast intent specially reference a type of BroadcastReceiver, that would work.

    So if you defined a class MyReceiver extends BroadcastReceiver inside your VideoActivity and then formed the broadcast intent as new Intent(context, VideoActivity.MyReceiver.class) that would be delivered.

    Hope that makes sense.