1

hi i have an alarm manager and user sets the alarms and i save that into a database for when the phone reboot or boot i write this so far but its not working

public class RestartAlarmsReceiver extends BroadcastReceiver {
private static final String TAG = "alarm";
AlarmManager alarmManager;
Intent mintent;
PendingIntent pendingIntent;
AlarmDbHelper dbHelper;
long time;
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")|| intent.getAction().
            equals("android.intent.action.REBOOT")) {

        dbHelper = new AlarmDbHelper(context);
        List<ChildTour> alarms =dbHelper.getAlarms();


        for (int i =0;i<2;i++){
            alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            mintent = new Intent(context,AlarmReceiver.class);
            ChildTour Child = alarms.get(i);
            mintent.putExtra("name",Child.getTime());
            mintent.putExtra("tourId",Child.getTourId()+"");
            pendingIntent = PendingIntent.getBroadcast(context,Child.getTourId(),mintent,0);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, Child.getHour());
            calendar.set(Calendar.MINUTE, Child.getMin());

            time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
            if(System.currentTimeMillis()>time)
            {
                if (calendar.AM_PM == 0)
                    time = time + (1000*60*60*12);
                else
                    time = time + (1000*60*60*24);
            }
            alarmManager.set(AlarmManager.RTC_WAKEUP,time, pendingIntent);
            Log.i("alarm","Alarm set With Id : "+Child.getTourId() +"Hour: " +Child.getHour()+"Minute: "+Child.getMin());
        }
}
}

and this is my manifests file i try Reboot and Turn off on my phone and that not working

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

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

    <receiver android:name=".RestartAlarmsReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.REBOOT"/>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        </intent-filter>
    </receiver>

and my alarmReciver class is working

  • so what error you getting..now – Dilip Dec 20 '17 at 04:51
  • On which device you tested where reboot receiver not getting called ? – ADM Dec 20 '17 at 04:51
  • @Dilip no errors nothing –  Dec 20 '17 at 04:53
  • @ADM i test it on my physical device and yea not geeting called on Reboot and Boot Completed –  Dec 20 '17 at 04:53
  • Thats what i asked which device which APi level ? Some manufacturer and now days most of them have **AutoStart** settings to allow/disallow apps to run in background. So if **AutoStart** is OFF then you may not get BootReceive Broadcast . I faced the same issue in Xiaomi ,and Oppo And some other manufacturers too . – ADM Dec 20 '17 at 04:56
  • And `ACTION_POWER_CONNECTED` is not related to BOOT process so you can remove this . – ADM Dec 20 '17 at 04:57
  • @ADM Samsung Galaxy J7 Prime Api 23 Android 6.0.1 –  Dec 20 '17 at 05:01

3 Answers3

1

Your Manifest action should look like .

 <receiver
        android:name=".receivers.BootCompleteReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="1000">
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="android.intent.action.REBOOT"/>
        </intent-filter>
    </receiver>

As i stated above Auto start option can be a issue of not getting reboot broadcast. So test with some other devices too.If the Device has Auto start then Enable it for your application . Settings > permissions > Autostart. This can be access only manually i.e there is no API to change enable/disable it programmatically. You can check these links for more Link1, Link2/

ADM
  • 20,406
  • 11
  • 52
  • 83
0

I got mine working but it was not easy, also I think the tablet/phone may or not need to be rooted

Try you manifest like this :

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service android:name="SendDataService" />
    <receiver
        android:name=".BootReciever">

        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

        </intent-filter>
    </receiver>
Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25
0

You will need to add a boot receiver in your manifest like this

manifest:

<receiver android:name=".OnBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
    </intent-filter>
</receiver>

And then create the boot receiver class like this...

receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context ctxt, Intent intent) {
AlarmHelper.setAlarm(ctxt);
}
}

My alarm helper class is a simple start of day alarm like this...

alarm helper class:

public class AlarmHelper {

public static void testAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.SECOND, 10);
setAlarm(context, when);    
}

public static void setAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.DAY_OF_YEAR, 1);
when.set(Calendar.HOUR_OF_DAY, 0);
when.set(Calendar.MINUTE, 0);
when.set(Calendar.SECOND, 0);
setAlarm(context, when);
}

    @SuppressLint("SimpleDateFormat")
private static void setAlarm(Context context, Calendar when) {

SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());

Boolean showNotifications = prefs.getBoolean("PREF_SHOW_NOTIFICATIONS",
false);

if (showNotifications) {    
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntent(context.getApplicationContext()));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Log.i(TAG, "Alarm set " + sdf.format(when.getTime()));
}
}
Dilip
  • 2,622
  • 1
  • 20
  • 27