问题
This is a fairly common question but I'm losing my mind. I think I've been thorough with the requirements. I want a BroadcastReceiver to do something when the phone restarts.
My BroadcastReceiver (for Booting):
public class BootReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("BootupReceiver", "Refreshing alarms.");
}
}
Within my Manifest's 'manifest' tag:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Within my Manifest's 'application' tag:
<receiver
android:name=".receivers.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
After deploying the app, I close it, then restart it manually. I don't think the stopped state is an issue.
Additionally
I have tried various combinations of this monstrosity:
<receiver
android:name=".receivers.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
I have also tried using the absolute package name in place of the relative one when referencing the receiver name.
I have additionally created a simple, standalone app with nothing but a MainActivity and the above BroadcastReceiver, which is working (infuriatingly).
I've tested this out on a Moto G and a Samsung Galaxy Edge 7.
Is there any known instance where a BootReceiver will not start? Any inclination as to what is going wrong for my code?
I have found a similar question that went unanswered.
EDIT
I have additionally tried using the ads shell to fire the REBOOT intent manually. Still no success.
EDIT 2
As requested the full manifest. If you see REDACTED I just removed something potentially confidential.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.REDACTED.REDACTED">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".REDACTEDApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.activities.SplashActivity"
android:label="@string/app_name"
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=".ui.activities.InitialActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.LoginActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.CreateAccountActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ui.activities.VideoPlayerActivity"
android:screenOrientation="landscape"/>
<activity
android:name=".ui.activities.DisclaimerActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.EPWebViewActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.EscapePlanActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.EscapeInfoActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.EscapeSessionActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activities.EscapeDetailActivity"
android:screenOrientation="portrait"/>
<meta-data
android:name="io.fabric.ApiKey"
android:value="REDACTED"/>
<activity
android:name=".ui.activities.RemindersActivity"
android:screenOrientation="portrait">
</activity>
<receiver android:name=".receivers.NotificationsReceiver"/>
<receiver
android:name=".receivers.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
EDIT 3
I tried adding another Receiver but have Android Studio generate it. Still no luck.
Used ADB Shell to fire the BOOT_COMPLETED at the app specifically. No success there either.
回答1:
After cleaning, rebuilding and regenerating the APK, it is now working. I'm not sure if Studio was caching an old APK, or if I misunderstand/messed up the regular build, but the receiver is now working.
来源:https://stackoverflow.com/questions/41963960/boot-broadcast-receiver-reboot-issues