Skip to content Skip to sidebar Skip to footer

Alarm With Broadcast Receiver Not Working On Fragment

I'm working on a app with Fragments, One of the fragment includes alarm functionality. For this I have used broadcast receiver but don't understand why its not working: Here is th

Solution 1:

Why don't you try a custom BroadcastReceiver in place of your inline implementation? Try this

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm time has been reached", Toast.LENGTH_LONG).show();
    }
}

Register the receiver in AndroidManifest

<receiver android:name=".AlarmReceiver" android:process=":remote"/>

set the alarm in your SchedulerListFragment.java

Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

AlarmManager alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));
alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + result , pendingIntent);

If you wish, you may register the BroadcastReceiver dynamically using registerReceiver() by passing an empty IntentFilter. That will also work without any issues.


Post a Comment for "Alarm With Broadcast Receiver Not Working On Fragment"