0

I've used an alarmManager to send daily notification. Since alarmManager stops after restarting phone so I've created a BroadcastReceiver to trigger on BOOT_COMPLETE, still no success. Even not getting toast.

BroadcastRreceiver class

package com.aman.dailynoti;

import...




public class BReceiver extends BroadcastReceiver {

 @Override

public void onReceive(Context context, Intent intent) {



    if ((intent.getAction()).equals("android.intent.action.BOOT_COMPLETED")) {

        Toast.makeText(context, "broadcast", Toast.LENGTH_SHORT).show();


        SharedPreferences mpreferences=context.getSharedPreferences("myPreferences",MODE_PRIVATE);
            int h=mpreferences.getInt("hour",14);

            int m=mpreferences.getInt("minute", 30);
        Calendar calendar=  Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,h);
        calendar.set(Calendar.MINUTE,m);
        calendar.set(Calendar.SECOND,00);

        Intent notiIntent = new Intent(context, Notification_Receiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

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

        }

  }
 }

AndroidManifest.xml

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

 <application...>
       <receiver
        android:name="com.aman.dailynoti.BReceiver"
        android:enabled="true"
        android:label="breceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        </receiver>

     </application>
iamanbansal
  • 2,412
  • 2
  • 12
  • 20

2 Answers2

0

Dont quote me on this but I believe android only gives a small window of time for your applications to do what they need to do on boot. If you want the user to receive notifications when not using the app look into push notifications.

yfdgvf asdasdas
  • 170
  • 1
  • 9
0

you should use also intent filter android.intent.action.QUICKBOOT_POWERON to recieve after restart