Broadcastreceiver 开机后不工作

Broadcastreceiver not working after boot

我创建了一个应用程序,它将接收来自 firebase 的通知,因此希望在用户启动 phone 后启动应用程序服务,这样他们就不需要再次手动启动应用程序。但是,广播接收器似乎无法正常工作。

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(广播接收器)

 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);
        }
    }

我检查了以下内容

  1. 已声明权限 RECEIVE_BOOT_COMPLETED 但不在标记内
  2. 接收者标签写入正确

我是做错了还是还遗漏了什么?我是否必须在我怀疑应该调用的 mainAcitivity 中调用它?非常感谢任何指导。

找到解决方案,这是一个非常愚蠢的理由。

我正在使用 Oppo 的 phone 进行测试,Oppo 有自己的安全管理器应用程序,您必须手动允许特定应用程序在启动后自动启动。就这些了。

我怀疑大多数 android phones 也有这个功能因此记得检查是否有这样的应用程序,如果有则提醒用户在安全中允许应用程序在他们开始重新启动 phone 并且无法使用您的应用程序打算提供的任何服务之前,Manager App。

希望对您有所帮助!

试试这个,对我有用。

    <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>