BroadcastReceiver没有从警报中调用onReceive

时间:2015-10-16 03:38:39

标签: android android-intent alarmmanager

我已经被困在这里好几个小时,看起来像是一个微不足道的问题。 我的BroadcastReceiver似乎根本没有收到警报广播。我可以看到使用“adb shell dumpsys alarm”设置警报,但我的onReceive方法永远不会被调用。

这是班级:

public class AlarmReceiver extends BroadcastReceiver {

public void AlarmReceiver(){
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("Alarm:", "Received alarm!");
}

这是我的MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scheduleAlarm();
}

private void scheduleAlarm(){

    Intent intent = new Intent(this,AlarmReceiver.class);

    final PendingIntent pIntent = PendingIntent.getBroadcast(this,19522,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pIntent);

    Log.i("Main", "Alarm Scheduled");
}

我的清单文件包含:

<receiver android:name="dt.wirelessproject.AlarmReceiver"></receiver>

1 个答案:

答案 0 :(得分:2)

试试这个......

您的Receiver类构造函数可能是错误的。只需改变你的接收器类......

public class AlarmReceiver extends BroadcastReceiver {

  public AlarmReceiver(){ 
    super();
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i("Alarm:", "Received alarm!");
  }
}