BroadcastReceiver not working when app is not running

▼魔方 西西 提交于 2019-11-29 13:53:12

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>  

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 );

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

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.

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.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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!