簡體   English   中英

重啟后的 Android 廣播接收器未運行

[英]Android After Reboot Broadcast Receiver is not running

我使用了這個權限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

接收者是:

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

代碼中的接收器是:

@Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");

        if (intent != null) {
            String action = intent.getAction();

        switch (action) {
            case Intent.ACTION_BOOT_COMPLETED:
                System.out.println("Called on REBOOT");
                // start a new service and repeat using alarm manager

                break;
            default:
                break;
        }
    }
}

重新啟動后,它仍然沒有被棒棒糖調用,但在棉花糖上它正在運行。

嘗試將此行放在接收器的意圖過濾器中。

<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />

如果您的應用程序安裝在 SD 卡上,您應該注冊它以獲取 android.intent.action.BOOT_COMPLETED 事件。

更新:由於您的應用程序正在使用警報服務,因此不應將其安裝在外部存儲上。 參考: http : //developer.android.com/guide/topics/data/install-location.html

每當平台啟動完成時,就會廣播一個帶有 android.intent.action.BOOT_COMPLETED 動作的意圖。 您需要注冊您的應用程序才能接收此意圖。 對於注冊,將此添加到您的 AndroidManifest.xml

<receiver android:name=".ServiceManager">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

因此,您將使用 ServiceManager 作為廣播接收器來接收啟動事件的意圖。 ServiceManager 類應如下所示:

public class ServiceManager extends BroadcastReceiver {

    Context mContext;
    private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
                // All registered broadcasts are received by this
        mContext = context;
        String action = intent.getAction();
        if (action.equalsIgnoreCase(BOOT_ACTION)) {
                        //check for boot complete event & start your service
            startService();
        } 

    }


    private void startService() {
                //here, you will start your service
        Intent mServiceIntent = new Intent();
        mServiceIntent.setAction("com.bootservice.test.DataService");
        mContext.startService(mServiceIntent);
    }
}

由於我們正在啟動服務,因此在 AndroidManifest 中也必須提及它:

<service android:name=".LocationService">
    <intent-filter>
        <action android:name="com.bootservice.test.DataService"/>
    </intent-filter>
</service>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM