BroadcastReceiver does not work when the application does not work

In my manifest file, I declared the recipient. (in the following way)

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

however, as soon as I turn off my application, I cannot receive warnings and notifications. Apparently, the OnReceive call in my Broadcast receiver never made.

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

Inside MainActivity, my alarm manager class is as 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 follows:

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

What should I do to receive notifications / alarms even if I turn off my application. Background service?

+6
source share
6 answers

you must add an intent filter to the manifest since

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

As Hong Kong says, you need to announce what events your receiver is listening to.

For instance:

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

and then when your alarm is set, use the 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 ); 
0
source

Your code is working fine!

All you have to do is change this line:

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

Using this line:

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

And the code in "onReceive" will work after 5000 ms (5 seconds), even if the application does not work

0
source

In my understanding, in some cases, depending on the implementation method, the OS has the right to set the alarm time. So try using AlarmManager.set (...), AlarmManager.setexact (...) etc. Respectively. In some cases, depending on the manufacturer (Android user OS), there is a possibility that the OS blocks a fire alarm.

0
source

Adding android:exported="true" for the recipient to the manifest file helped me to receive alarms (and thus wake the application) even when the application was disconnected (intentionally with me, removing the application from the task list).

0
source

1. Install the receiver in the manifest file:

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

Always remember that the entire Android system is case sensitive. Therefore, check the spelling in AndroidMainfest.xml.

2. If you are creating a PendingIntent for your recipient, add requestCode - even this is a random number! Without your code, onReceive never called!

The function starting AlarmManager should look like this:

 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! } } 

Original article: Why is my BroadcastReceiver not called?

-1
source

Source: https://habr.com/ru/post/944352/


All Articles