8

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 ?

Pankaj
  • 7,908
  • 6
  • 42
  • 65
tony9099
  • 4,567
  • 9
  • 44
  • 73
  • please check https://stackoverflow.com/questions/16824341/keep-broadcast-receiver-running-after-application-is-closed and https://github.com/twilio/voice-quickstart-android/issues/150 – Developer Nov 22 '18 at 04:31

6 Answers6

0

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>  
0

As Gong Cong says, you need to declare which events your receiver should listen.

For example :

<receiver android:name=".OnAlarmReceive"> 

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

and then when your set your alarm, use an intent with your action :

Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast( this, 0, intent, 0 );
nbe_42
  • 1,212
  • 1
  • 14
  • 22
  • I did that. However, once i turn off my activity, the alarm does not go off. with the app running the code above (even after my changes) work. but the moment i remove the app (from task manager) it does not run. – tony9099 May 06 '13 at 09:10
  • Can you post a part of your code ? How do you set your alarm ? Do you use AlarmManager.RTC_WAKEUP flag ? – nbe_42 May 06 '13 at 09:16
  • yes I do, however like this, i can not put an extra with the intent as I am not calling an activity with the intent rather the broadcast receiver in the manifest no ? – tony9099 May 06 '13 at 09:27
  • It's strange, your code seems clean... Here is a complete example for what you need, try the code in your project http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/ – nbe_42 May 10 '13 at 09:46
  • I read somewhere that after 3.1 if you force shut your application the broadcast receiver will not work. – tony9099 May 10 '13 at 12:30
  • Mmmh, try to add this flag in your Intent : http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES – nbe_42 May 10 '13 at 12:50
0

Your code is working fine!

All you have to do is to change this line:

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

With this line:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
SystemClock.elapsedRealtime() + 5000, pendingIntent);

And the code in the "onReceive" will run after 5000ms (5sec) even when app is not running

David
  • 37,109
  • 32
  • 120
  • 141
0

In My understanding, In some cased depending on the way of implementations, OS has authority to adjust the alarm set time. So try to use AlarmManager.set(...), AlarmManager.setexact(...) etc accordingly. In Some cases, depending on the manufacturer(Custom Android OS), there is a possibility that the OS is blocking fire alarm.

Nithinjith
  • 1,775
  • 18
  • 40
0

Adding android:exported="true" for receiver in manifest file helped me to receive alarms (and thus, wake the application) even when application was shut-down (intentionally by me, removing app from task list).

-1

1.Declare the receiver in the Manifest-file:

<receiver android:name="your.package.name.TestAlarmReceiver"></receiver>

Always remember that the whole Android-System is case sensitive. So check your spelling is correct in the AndroidMainfest.xml.

2.If you create a PendingIntent for your Receiver, please add an requestCode - even it is a random number! Without your onReceive code never get called!

The function which start AlarmManager should look like below:

public static void scheduleTestAlarmReceiver(Context context) {
   Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
   PendingIntent sender = PendingIntent.getBroadcast(context, 123456789,  receiverIntent, 0);

   AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender);
}

BroadcastReceiver class:

package your.package.name;

public class TestAlarmReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent arg1) {
     // your code here!
   }
}

The original article: Why my BroadcastReceiver does not get called?

Nolesh
  • 6,848
  • 12
  • 75
  • 112