Search code examples
androidbroadcastreceiveralarmmanager

Android: My Alarm isn't being Started by my BOOTCOMPLETED receiving class


I have a problem I haven't been able to figure out. I have a repeating alarm broadcast receiver and I've set up another broadcast receiver to receive the BOOT_COMPLETE signal in order to restart my alarm upon booting. The code looks like everyone else's that I've looked up, so I'n not sure what's wrong.

My Boot receiver:

public class DeviceBootReceiver extends BroadcastReceiver {
Context context2;

public boolean fileExistance(String fname){
    File file = context2.getApplicationContext().getFileStreamPath(fname);
    return file.exists();
}




public String loadCurrentWhat (String filename){
    FileInputStream fileInputStream;
    String info = "";
    try {
        fileInputStream = context2.getApplicationContext().openFileInput(filename);
        int count;
        while ((count=fileInputStream.read()) != -1){
            info += Character.toString((char)count);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return info;
}

@Override
public void onReceive(Context context, Intent intent) {
    context2 = context;
        if (fileExistance("number")){
                 /* Setting the alarm here */
                Intent alarmIntent = new Intent(context, AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

                AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                int interval = 10000;
                manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        }

}
}

My Repeating alarm:

public class AlarmReceiver extends BroadcastReceiver {


Context context2;

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

    // For our recurring task, we update the character data in internal storage.
    context2 = context.getApplicationContext();
    UPDATE();

}
}

My Manifest:

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver
        android:name=".DeviceBootReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

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



</application>

Solution

  • From what I am able to see so far, you need to put the RECEIVE_BOOT_COMPLETED permission outside the application tag.

    Try this. This should work.