Android 未调用BroadcastReceiver的onReceiver,AlarmManager

Android 未调用BroadcastReceiver的onReceiver,AlarmManager,android,alarmmanager,Android,Alarmmanager,我正在建立一个出租车预订应用程序,我需要每20秒出租车的当前位置 我定义了一个AlarmManager,需要它每20秒重复一次。但它不会经常重复。相反,它在233秒后重复了自己,而且只重复了一次。我做错了什么 我的主屏幕有一个内部类OnAlarmReceiver,在创建主屏幕时,我将呼叫AlarmManager AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent i =

我正在建立一个出租车预订应用程序,我需要每20秒出租车的当前位置

我定义了一个AlarmManager,需要它每20秒重复一次。但它不会经常重复。相反,它在233秒后重复了自己,而且只重复了一次。我做错了什么

我的主屏幕有一个内部类OnAlarmReceiver,在创建主屏幕时,我将呼叫AlarmManager

    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, OnAlarmReceiver.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 20);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            cal.getTimeInMillis(), God.UPDATE_PENDING_INTERVAL, pi);
主屏幕中的内部类

public class OnAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // PullPendingRequests.acquireStaticLock(context);
        Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG)
                .show();
        Log.d("Taxeeta:PullPendingRequets", "CallService Location");
        context.startService(new Intent(context, PullPendingRequests.class));
    }
}
我的AndroidManifest文件已被删除

    <service
        android:name="com.taxeeta.support.PullPendingRequests"
        android:enabled="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" />

    <receiver android:name=".com.taxeeta.HomeScreen.OnAlarmReceiver" />
</application>
adb外壳转储系统报警的输出| grep taxeta

 ELAPSED_WAKEUP #7: Alarm{409303b0 type 2 com.taxeeta}
    operation=PendingIntent{408ba2d8: PendingIntentRecord{40887be8 com.taxeeta broadcastIntent}}
 com.taxeeta
    5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver

这段代码对我很有用,请确保在android清单文件中添加了Receiver

AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
                         PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 20 seconds after boot completed
cal.add(Calendar.SECOND, 20);
//
// Fetch every 20 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                       cal.getTimeInMillis(), 1000*20, pending);

为了修复它,我删除了内部类OnAlarmReceiver并修复了androidmanifest.xml文件

    <receiver
        android:name="com.taxeeta.support.OnAlarmReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>

如果上述答案对您不起作用,那么当
AlarmManager
触发过期报警时,还有另一种方法可以接收任何回调。您只需检查一下:在实例化
pendingent
时发送错误的
Intent
。例如,您希望在其中一个接收者上接收呼叫
onReceive
,但您通过
getActivity
getService
实例化了一个
pendingent
,但您实际的意思是
getReceiver

创建
pendingent
实例时,有多种方法可以创建它(
getService
getActivity
getReceiver
getForegroundsService

如果您想要
活动
意图的接收者,那么您:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);
如果您想要
BroadcastReceiver
目标的接收者:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);
如果您想要前台
服务
目标接收者:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);
如果您想要
服务
目的接收者:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);
此外,确保您的意图指向正确的类(例如,为活动、服务等创建意图)。如果您这样错误地传递,您将不会接到任何呼叫:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 
我也发布了类似的答案


HTH

上述解决方案对我不起作用

此外,通过代码动态注册也实现了以下目的:

Intent intent = new Intent();
intent.setAction("android.intent.action.NOTIFY");

//Register the receiver
context.registerReceiver(new OnAlarmReceiver(),new IntentFilter());

不,不起作用。OnAlarmReceiver,onReceive也没有被调用。同样的,alarmmanager上的任何东西对我都不起作用。我尝试了所有的建议。不知道该做什么很奇怪,那是很久以前的事了,甚至不知道我需要它做什么。对不起,伙计,如果我记得的话,我会发布它。这^..当你使用alarmmanager的接收器时需要使用
PendingIntent.getBroadcast(…)
而且工作起来很神奇;)