How to use registerReceiver method?


Question

I want to use dynamically registered BroadcastReceiver that has a reference to an Activity so it can modify its UI. I am using Context.registerReceiver() method but receiver's onReceive() method is never called.

Here is the sample code showing the problem:

package com.example;

import android.app.Activity;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

public class RegisterBroadcastReceiver extends Activity {

    public static class MyIntentService extends IntentService {

        public MyIntentService() {
            super(MyIntentService.class.getSimpleName());
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            Intent i = new Intent(this, MyBroadcastReceiver.class);
            sendBroadcast(i);
        }
    }

    public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(MyBroadcastReceiver.class.getSimpleName(),
                  "received broadcast");
        }

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter();
        registerReceiver(mReceiver, intentFilter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            Intent i = new Intent(this, MyIntentService.class);
            startService(i);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

}
1
45
6/30/2014 8:43:28 PM

The whole code if somebody need it.

void alarm(Context context, Calendar calendar) {
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);

    final String SOME_ACTION = "com.android.mytabs.MytabsActivity.AlarmReceiver";
    IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

    AlarmReceiver mReceiver = new AlarmReceiver();
    context.registerReceiver(mReceiver, intentFilter);

    Intent anotherIntent = new Intent(SOME_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, anotherIntent, 0);
    alramManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    Toast.makeText(context, "Added", Toast.LENGTH_LONG).show();
}

class AlarmReceiver extends BroadcastReceiver {     
    @Override
    public void onReceive(Context context, Intent arg1) {
        Toast.makeText(context, "Started", Toast.LENGTH_LONG).show();
    }
}
39
5/13/2018 7:36:07 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon