简体   繁体   中英

Broadcastreceiver not working after boot

I have created an apps that will receive notifications from firebase thus hoping to start the app service after user boot their phone so that they do not need to manually start the apps again. However, the broadcast receiver seems to just not working.

AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.simplifiedcoding.firebasecloudmessaging">

    <!-- Adding Internet Permission -->

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!--
            Defining Services
        -->
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <service android:name=".NotificationService"/>


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

    </application>

</manifest>

Broadcast.java (BroadcastReceiver)

 public class Broadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
                Intent service = new Intent(context, NotificationService.class);
                context.startService(service);
                Toast.makeText(context, "Broadcast started", Toast.LENGTH_LONG).show();


        }
    }

MainActivity.java

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

I have checked the followings

  1. Permission RECEIVE_BOOT_COMPLETED declared and not within tag
  2. receiver tag is being written correctly

Am I doing wrong or still missing something else? Do I have to call it at mainAcitivity which I doubt I should ? Any guidance are much appreciated.

Got the solution and it was a very dumb reason.

I'm using Oppo's phone to test and Oppo has its own Security Manager App where you have to manually allow specific apps to be start up automatically after boot. That's all.

I suspect most android phones has this feature as well thus bear in mind to check whether there is such app and if it does then remind the user to allow the apps in the Security Manager App before they start rebooting their phone and not able to use any service's your App intended to provide.

Hope this helps!

Try this, it worked for me.

    <receiver android:enabled="true" android:name=".Broadcast"
                  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM