Devendra Dagur
Devendra Dagur

Reputation: 850

BroadcastReceiver not called after close my application

I am trying to show a notification to user at fixed time. I am using BroadcastReceiver to show notification. Problem is that, BroadcastReceiver only called when application is running in foreground or background. But BroadcastReceiver not called after when I close application (means kill application). I am using AlarmManager to call BroadcastReceiver.

I read somewhere on stackoverflow that we don't to make service specifically to call BroadcastReceiver. We just need to register it in AndroidManifest.

I am using Xiomi Redmi 3s prime device and android version is 6.0.1.

So what I am missing? Why BroadcastReceiver not called?

This is my class from where I am triggering AlarmManager

public class Settings extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.settings,container,false);


        Intent intent = new Intent(getActivity(), BroadcastReciever.class);

        AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Service.ALARM_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 27);
        calendar.set(Calendar.SECOND, 00);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

        return view;
    }
}

This is my BroadcastReceiver

public class BroadcastReciever extends BroadcastReceiver {

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    @Override
    public void onReceive(Context context, Intent intent) {


        Intent nofificationIntent = new Intent(context, Home.class);
        nofificationIntent.putExtra("notify_key","notify_value");

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,1,
                        nofificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.donate)
                        .setSound(alarmSound)
                        .setContentIntent(resultPendingIntent)
                        .setContentTitle("Whats up")
                        .setContentText("Hello world");

        int mNotificationId = 001;

        NotificationManager mNotifyMgr =
                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());

    }
}

This is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.nf">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Home"
            android:windowSoftInputMode="stateHidden"
            android:screenOrientation="portrait"/>

        <activity android:name=".AndroidDatabaseManager"
            android:theme="@style/Theme.AppCompat.Light"/>

        <receiver android:name="broadcast.BroadcastReciever"
            android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
        </receiver>

    </application>

</manifest>

Upvotes: 1

Views: 971

Answers (2)

Abhay Kumar Jaiswal
Abhay Kumar Jaiswal

Reputation: 1

In Activity :-

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(MainActivity.this,MyFirstService.class));
}

In Service :-

public class MyFirstService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

@Override
public void onCreate() 
{
    super.onCreate();
    Notification note = new Notification(0,null,System.currentTimeMillis());
    note.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(42, note);
    stopService(new Intent(this, MyService.class));
    stopForeground(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    return   START_STICKY;
}

}

In Broadcast :-

In broadcast write the code after create serivce .

Upvotes: 0

Tahmid Rahman
Tahmid Rahman

Reputation: 758

Use WakefulBroadcastReceiver instead

Helper for the common pattern of implementing a BroadcastReceiver that receives a device wakeup event and then passes the work off to a Service, while ensuring that the device does not go back to sleep during the transition.

This class takes care of creating and managing a partial wake lock for you; you must request the WAKE_LOCK permission to use it.

See the documentation here

Hope it helps!!

Upvotes: 2

Related Questions