I'm working on application where I need to set alarm on reboot to start a service on specific interval.
I've created required Boot Complete Receiver class, added uses-permission and defined receiver in manifest file. I've tested on many real and simulated devices on every device, receiver works except Hisense device with Android 5.1.1
I've already tried these solutions.
broadcastreceiver-not-receiving-boot-completed
boot-completed-not-working-android
android-boot-completed-not-received-when-application-is-closed
I've also removed android:installLocation="auto"
and added android:directBootAware="true" android:enabled="true" android:exported="true"
suggested in some thread.
Please find Receiver class and manifest code below.
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Set instance Alarm on boot
Calendar calendar = Calendar.getInstance();
Intent messageCheckerIntent = new Intent(context, MessageCheckerService.class);
PendingIntent pendingIntent =
PendingIntent.getService(context, REQ_MESSAGE_CHECK,
messageCheckerIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Cancel if any existing alarm for given pendingIntent (MessageCheckerService)
alarmManager.cancel(pendingIntent);
// Alarm is reset every time when this service is called,
// still keep alarm repeating so if one call fails does not affect following calls
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ALARM_INTERVAL, pendingIntent);
}
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.project.myapp">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:launchMode="singleTask"
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=".MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask">
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".GPSTrack" />
<service
android:name=".MessageCheckerService"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<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="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>