How to activate the BroadcastReceiver after rebooting?

1

I explain my problem, I have created an application that has to send a notification every day at a specific time. For this I have developed the following code:

Intent intent  = new Intent(this, Alertas.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 1, intent,  PendingIntent.FLAG_CANCEL_CURRENT);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set (Calendar.HOUR_OF_DAY, Integer.valueOf(hora[0]));
cal.set(Calendar.MINUTE, Integer.valueOf(hora[1]));
cal.set(Calendar.SECOND, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);

Where Alerts is a BroadcastReceiver where the following summary code is executed:

                NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(farmacia.getNombre())
                        .setContentText(farmacia.getDireccion())
                        .setLights(Color.CYAN, 1000, 500)
                        .setVibrate(new long[]{1000, 500, 2000, 500, 1000});

        Intent resultIntent = new Intent(context, Inicio.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(Inicio.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

When I test the emulator, changing the date manually works correctly, on the physical device as well, but when I restart the device, these alerts stop being notified at the indicated time, it is not displayed directly, it only does it the first time when I activate the alerts, I hope you can help me, it's for my final project, thank you very much!

    
asked by Antonio de los 19.05.2016 в 17:14
source

3 answers

1

Remember that the BroadcastReceiver you have to register, it is usually done in the onResume() method:

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

  // Registra el broadcast receiver para recibir mensajes.
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("mi-evento"));
}

and very important to register it, you can do it in the onPause() method:

    @Override    
    protected void onPause() {
      // Desregistrar cuando la actividad no es visible!.
      LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
      super.onPause();
    } 
    
answered by 19.05.2016 в 17:34
1

Add the following line in <manifest>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
answered by 19.05.2016 в 17:44
0

Some time ago I saw myself with a problem similar to yours. I'll explain how I solved it.

First, separate your BroadCastReceiver into a separate class.

public class MusicBroadcastReceiver extends BroadcastReceiver {

    static MainActivity mainVar = null; // Esta será la referencia a la clase del MainActivity

    public MusicBroadcastReceiver() {}

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


    }

    public static void setMainActivityHandler(MainActivity main){
        mainVar = main;
    }

}

Then instantiate it in the Main of your application. In my case, my object was called MusicBroadcastReceiver.

BroadcastReceiver broadcastReceiver = new MusicBroadcastReceiver();
        MusicBroadcastReceiver.setMainActivityHandler(this);    // Le pasamos este activity para vincularlos
        IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.ANY_ACTION");
        registerReceiver(broadcastReceiver, callInterceptorIntentFilter);

Second, declare the BroadcastReceiver in your manifest.

<receiver
    android:name=".MusicReceiver.MusicBroadcastReceiver"
    android:exported="true" >
    <intent-filter>
        <action android:name="com.android.music.metachanged" />
        <action android:name="com.htc.music.metachanged" />
        <action android:name="fm.last.android.metachanged" />
        <action android:name="com.sec.android.app.music.metachanged" />
        <action android:name="com.nullsoft.winamp.metachanged" />
        <action android:name="com.amazon.mp3.metachanged" />
        <action android:name="com.miui.player.metachanged" />
        <action android:name="com.rhapsody.metachanged" />
        <action android:name="com.real.IMP.metachanged" />
        <action android:name="com.maxmpz.audioplayer.metachanged" />
        <action android:name="com.sonyericsson.music.metachanged" />
        <action android:name="com.rdio.android.metachanged" />
        <action android:name="com.samsung.sec.android.MusicPlayer.metachanged" />
        <action android:name="com.andrew.apollo.metachanged" />
        <action android:name="com.spotify.mobile.android.metadatachanged" />
        <action android:name="com.spotify.music.metadatachanged" />
    </intent-filter>
</receiver>

You have an example in my repository:

link

I hope it serves you.

    
answered by 31.05.2016 в 11:28