BroadcastReceiver not working when app is not running

tony9099 picture tony9099 · May 6, 2013 · Viewed 9.6k times · Source

In my manifest file I have declared the receiver. (as follows)

<receiver android:name=".OnAlarmReceive" />

however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.

public class OnAlarmReceive extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
   {
       //various stuff
   }
}

Inside the MainActivity, my alarm manager class is as the follows.

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent("MY_ALARM_NOTIFICATION");
    intent.setClass(this, OnAlarmReceive.class);
    intent.putExtra("message", message);
    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(MainActivity.this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar timeCal = Calendar.getInstance();
    timeCal.set(Calendar.HOUR_OF_DAY, hour);
    timeCal.set(Calendar.MINUTE, minutes);

    alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);

and my manifest as is follows :

    <receiver android:name=".OnAlarmReceive">
    <intent-filter android:priority="1">  
        <action android:name="MY_ALARM_NOTIFICATION"/>  
    </intent-filter>  
</receiver>  

What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?

Answer

Gong Cong picture Gong Cong · May 6, 2013

you should add intent-filter in manifest,as

receiver android:name=".SmsBroadCastReceiver">  
        <intent-filter android:priority="20">  
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
        </intent-filter>  
    </receiver>