android接收器没被调用

时间:2017-09-12 15:43:51

标签: java android broadcastreceiver

我有一个Android应用程序,我有一个来自WakefulBroadcastReceiver的接收器,它永远不会被调用。

我在AlaramReceiver.java上放了一个断点,但从未介入过。 我检查了案例以及拼写接收者类名的方式。

我将添加我的清单和链接的java类。

感谢您的帮助。

清单:

 <receiver android:name="fr.cls.mobility.myclsdroiddata.service.android.OnAlarmReceiver_">

AlarmReceiver_.class:

public final class OnAlarmReceiver_
    extends OnAlarmReceiver
{


}

AlamarReceiver.java:

@EReceiver
public class OnAlarmReceiver extends WakefulBroadcastReceiver {

    private static final String LOG_MESSAGE_ON_RECEIVE = "startWakefulService with context and DataDownloadIntentServiceFactory.getIntentAlarmReceived";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(OnAlarmReceiver.class.getSimpleName(), LOG_MESSAGE_ON_RECEIVE);
        startWakefulService(context, DataDownloadIntentServiceFactory.getIntentAlarmReceived(context, null));
    }

}

DataDownloadIntentServiceFactory.java:

package fr.cls.mobility.myclsdroiddata.service.android;

import com.effitic.delegates.Action;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.os.ResultReceiver;

/**
 * Cette classe crée les différents services Android de mise à jour des positions. <br>
 * <br>
 * Copyright : Copyright (c) 2013 <br>
 * <br>
 * Société : CLS (Collecte Localisation Satellites)
 * 
 * @author Effitic
 * @version Revision: 1.1.0.02 - Date: 2014-01-15
 */
public final class DataDownloadIntentServiceFactory {

    /**
     * Constructeur.
     */
    private DataDownloadIntentServiceFactory() {
        super();
    }

    public static Intent getIntentNetworkStateChanged(Context context, boolean networkAvailable) {
        Intent dataDownloadIntentService = createIntent(context, null, DataDownloadIntentService.NETWORK_STATE_CHANGED);
        dataDownloadIntentService.putExtra(DataDownloadIntentService.NETWORK_STATE_CHANGED, networkAvailable);
        return dataDownloadIntentService;
    }

    private static Intent createIntent(Context context, ResultReceiver resultReceiver, String type) {
        Intent dataDownloadIntentService = DataDownloadIntentService_.intent(context).get();
        dataDownloadIntentService.putExtra(DataDownloadIntentService.TYPE, type);
        dataDownloadIntentService.putExtra(DataDownloadIntentService.RESULT_RECEIVER, resultReceiver);
        return dataDownloadIntentService;
    }

    public static Intent getIntentAlarmReceived(Context context, final Action<Object> refreshFinishedHandler) {
        ResultReceiver resultReceiver = new ResultReceiver(null) {
            @Override
            protected void onReceiveResult(int resultCode, android.os.Bundle resultData) {
                if (refreshFinishedHandler != null) {
                    refreshFinishedHandler.execute(null);
                }
            };
        };

        Intent dataDownloadIntentService = createIntent(context, resultReceiver, DataDownloadIntentService.ALARM_RECEIVED);
        return dataDownloadIntentService;
    }

    public static Intent getIntentRefresh(Context context, final Action<Object> refreshFinishedHandler) {
        ResultReceiver resultReceiver = new ResultReceiver(null) {
            @Override
            protected void onReceiveResult(int resultCode, android.os.Bundle resultData) {
                if (refreshFinishedHandler != null) {
                    refreshFinishedHandler.execute(null);
                }
            };
        };
        Intent dataDownloadIntentService = createIntent(context, resultReceiver, DataDownloadIntentService.REFRESH);
        return dataDownloadIntentService;
    }

    public static Intent getIntentBootCompleteReceived(Context context) {
        Intent dataDownloadIntentService = createIntent(context, null, DataDownloadIntentService.BOOT_COMPLETE_RECEIVED);
        return dataDownloadIntentService;
    }

    public static <T extends ResultReceiver> T getResultReceiverFromIntent(Intent intent, Class<T> clazz) {
        Parcelable o = intent.getParcelableExtra(DataDownloadIntentService.RESULT_RECEIVER);
        if (clazz.isInstance(o)) {
            return clazz.cast(o);
        }
        return null;
    }
}

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:versionCode="51" android:versionName="1.3.01-SNAPSHOT" package="fr.cls.mobility.myclsdroiddata">

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="14"/>
    <!-- permissions -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- pour le stockages des tuiles de carto -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application android:allowBackup="false" android:debuggable="true" android:icon="@drawable/notif" android:label="@string/app_name" android:name="fr.cls.mobility.myclsdroiddata.view.application.CLSApplication_" android:theme="@style/AppTheme2">
        <activity android:configChanges="keyboardHidden|orientation|screenSize" android:hardwareAccelerated="false" android:label="@string/app_name" android:launchMode="singleTop" android:name="fr.cls.mobility.myclsdroiddata.view.activity.MapViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="fr.cls.mobility.myclsdroiddata.view.activity.PanelFragmentActivity" android:windowSoftInputMode="stateHidden"/>
        <service android:name="fr.cls.mobility.myclsdroiddata.service.android.DataDownloadIntentService_"/>
        <receiver android:name="fr.cls.mobility.myclsdroiddata.service.android.OnBootReceiver_">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <receiver android:name="fr.cls.mobility.myclsdroiddata.service.android.OnNetworkStateChangedReceiver_">
            <intent-filter>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
                <action android:name="android.net.wifi.STATE_CHANGE"/>
            </intent-filter>
        </receiver>
        <receiver android:name="fr.cls.mobility.myclsdroiddata.service.android.OnAlarmReceiver_"/>


    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

您尚未在清单中为接收者指定intent过滤器。没有intent过滤器,它不知道何时触发,或者你需要明确地调用它

<receiver android:name="fr.cls.mobility.myclsdroiddata.service.android.OnAlarmReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.YOUR_INTENT_ACTION" />
        <!--If you want to watch network connectivity state-->
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

否则你必须明确地调用它

Intent intent = new Intent(getApplicationContext(), fr.cls.mobility.myclsdroiddata.service.android.OnAlarmReceiver.class);
sendBroadcast(intent);`

答案 1 :(得分:0)

在我的PlanifyAlarmHandler

我有:

Intent alarmIntent = new Intent();
alarmIntent.setClassName("fr.cls.mobility.service.android", "OnAlarmReceiver_");

而不是

Intent alarmIntent = new Intent(context, OnAlarmReceiver_.class);

现在它正在运作。 谢谢你的所有答案。