繁体   English   中英

引导后广播接收器不工作

[英]Broadcastreceiver not working after boot

我创建了一个应用程序,该应用程序将从firebase接收通知,因此希望在用户启动手机后启动应用程序服务,以便他们无需再次手动启动应用程序。 但是,广播接收器似乎无法正常工作。

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

我检查了以下内容

  1. 声明为RECEIVE_BOOT_COMPLETED的权限,不在标记内
  2. 接收器标签被正确写入

我做错了还是还缺少其他东西? 我是否必须以mainAcitivity来称呼它,我怀疑我应该这么做吗? 任何指导都非常感谢。

得到了解决方案,这是一个非常愚蠢的原因。

我正在使用Oppo的电话进行测试,Oppo拥有自己的Security Manager App,您必须在其中手动允许特定的应用程序在启动后自动启动。 就这样。

我怀疑大多数android手机也具有此功能,因此请记住检查是否有这样的应用程序,如果确实存在,则提醒用户在开始重新启动手机之前无法在Security 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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