Broadcast Receiver
I have an app which repeats an alarm everyday but somehow after rebooting its not registering the broadcast receiver for Samsung Galaxy Grand Neo (Android 4.4.2) but is working fine for Sony Xperia ZR (Android 5.0.2)
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sap.wish"
android:installLocation="preferExternal" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:label="@string/title_activity_fullscreen"
android:screenOrientation="portrait"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/SettingsTheme" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name=".AlarmReceiver" />
</intent-filter>
</receiver>
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".StopAlarm"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
BootUpReceiver class
package com.sap.wish;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.Calendar;
public class BootUpReceiver extends BroadcastReceiver {
AlarmManager alarmManager;
PendingIntent pendingIntent;
SQLiteDatabase db;
String tableName = "SETTINGS";
final int ALARM_ID = 1;
final String id = "ID";
final String value = "VALUE";
public BootUpReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
db = context.getApplicationContext().openOrCreateDatabase("WTDatabase", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName
+ "(" + id + " INT PRIMARY KEY NOT NULL," +
value + " TEXT NOT NULL);");
Cursor c = db.rawQuery("SELECT * FROM " + tableName, null);
if (c.moveToFirst()) {
String result = c.getString(c.getColumnIndex(value));
long savedmilli = Long.parseLong(result);
Calendar savedCalendar = Calendar.getInstance();
savedCalendar.setTimeInMillis(savedmilli);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID, intent2, 0);
if (savedmilli != 0) {
while (System.currentTimeMillis() > savedmilli)
savedmilli = savedmilli + (24 * 60 * 60 * 1000);
db.execSQL("UPDATE " + tableName + " SET " + value + "='" + String.valueOf(savedmilli) + "' WHERE " + id + "=" + 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, savedmilli, pendingIntent);
}
}
c.close();
}
}
I think this is because your app is probably installed on the External SD Card, if so your
BOOT_COMPLETED
receiver will never get called.to fix this add:
to your receiver's intent filter like this:
Hope this will help you out, if it doesn't maybe change install location:
edit
Some devices got fast boot/reboot function and won't trigger the
BOOT_COMPLETED
receiver, in that case try using:Good luck