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