java - AlarmManager 从不在 AlarmReceiver/BroadcastReceiver 中调用 onReceive

标签 java android broadcastreceiver android-service alarmmanager

我仍然无法触发我的 AlarmReceiver 类的 onReceive 方法。此实现是否有任何问题?

所有这些应该做的就是等待一段时间(最好是 6 天),然后弹出一个通知。 (你能相信没有一个内置的系统吗?crontab 任何人!?)

MyActivity 和 BootReceiver 都在必要的条件下设置了警报。 AlarmService 踢出一个通知。 AlarmReceiver 应该 捕捉警报并启动 AlarmService,但它从未捕捉到该广播,无论我做什么都不会。

哦,我一直在我的 Droid X 2.3.4 上进行测试。针对 API 8 构建的项目。

附言其中大部分改编自 http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/

------------ MyActivity.java ------------

public class MyActivity extends Activity implements SensorEventListener {

    private void setupAlarm() {
        Log.i(TAG, "Setting up alarm...");
        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, AlarmReceiver.class), 0);

        // Get alarm trigger time from prefs
        Log.i(TAG, "Getting alarm trigger time from prefs...");
        SharedPreferences mPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
        long trigger = SocUtil.getLongFromPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER);
        Log.i(TAG, "Trigger from prefs: " + trigger + " (" + new Date(trigger).toString() + ").");

        // If alarm trigger is not set
        if(trigger == new Long(-1).longValue()) {
            // Set it
            trigger = new Date().getTime() + NOTIFY_DELAY_MILLIS;
            SocUtil.saveLongToPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER, trigger);
            Log.i(TAG, "Trigger changed to: " + trigger + " (" + new Date(trigger).toString() + ").");

            // And schedule the alarm
            alarmMgr.set(AlarmManager.RTC, trigger, pendingIntent);
            Log.i(TAG, "Alarm scheduled.");
        }
        // If it is already set
        else {
            // Nothing to schedule. BootReceiver takes care of rescheduling it after a reboot
        }
    }

}

------------ AlarmService.java ------------

public class AlarmService extends IntentService {

   public AlarmService() {
      super("AlarmService");
   }

   @Override
   public void onHandleIntent(Intent intent) {
      Log.i(AlarmConst.TAG, "AlarmService invoked.");
      this.sendNotification(this);
   }

   private void sendNotification(Context context) {
      Log.i(AlarmConst.TAG, "Sending notification...");
      Intent notificationIntent = new Intent(context, Splash.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

      NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(R.drawable.icon, "Test1", System.currentTimeMillis());
      notification.setLatestEventInfo(context, "Test2", "Test3", contentIntent);
      notificationMgr.notify(0, notification);
   }
}

------------ AlarmReceiver.java ------------

public class AlarmReceiver extends BroadcastReceiver {

   // onReceive must be very quick and not block, so it just fires up a Service
   @Override
   public void onReceive(Context context, Intent intent) {
      Log.i(AlarmConst.TAG, "AlarmReceiver invoked, starting AlarmService in background.");
      context.startService(new Intent(context, AlarmService.class));
   }
}

------------ BootReceiver.java ------------ (恢复已删除的警报,因为我在操作系统中安排的内容不够重要,无法在重启后保留 -_-)

public class BootReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Log.i(AlarmConst.TAG, "BootReceiver invoked, configuring AlarmManager...");


      Log.i(AlarmConst.TAG, "Setting up alarm...");
      AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, AlarmReceiver.class), 0);

      // Get alarm trigger time from prefs
      Log.i(AlarmConst.TAG, "Getting alarm trigger time from prefs...");
      SharedPreferences mPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
      long trigger = SocUtil.getLongFromPrefs(mPrefs2, AlarmConst.PREFS_TRIGGER);
      Log.i(AlarmConst.TAG, "Trigger from prefs: " + trigger + " (" + new Date(trigger).toString() + ").");

      // If trigger exists in prefs
      if(trigger != new Long(-1).longValue()) {
          alarmMgr.set(AlarmManager.RTC, trigger, pendingIntent);
          Log.i(AlarmConst.TAG, "Alarm scheduled.");
      }
   }
}

------------ list ------------

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

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
    </activity>

<receiver android:name="com.domain.app.BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<receiver android:name="com.domain.app.AlarmReceiver"></receiver>

    <service android:name="com.domain.app.AlarmService"></service>

最佳答案

这是我最近用来每小时发出一次通知的一些代码(在我的 MainActivity 中):

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent Notifyintent = new Intent(context, Notify.class);
PendingIntent Notifysender = PendingIntent.getBroadcast(this, 0, Notifyintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000, Notifysender);

然后在 Notify.java 中

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Notify extends BroadcastReceiver{

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
          NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
          Notification notification = new Notification(R.drawable.ic_launcher, "Update Device", 0);
          Intent notificationIntent = new Intent(context, MainActivity.class);
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
          notification.setLatestEventInfo(context, "Device CheckIn", "Please run Device CheckIn", contentIntent);
          notification.flags |= Notification.FLAG_HIGH_PRIORITY;
          myNotificationManager.notify(0, notification);
    }
}

然后最后在 AndroidManifest.xml 中,我在标签之间有这个:

<receiver android:name=".Notify" android:exported="true">
         <intent-filter>
                <action android:name="android.intent.action.NOTIFY" />
            </intent-filter>
</receiver>

我有我知道在办公室工作的主要代码,当我遇到同样的问题时,请随时给我发电子邮件寻求更多帮助。

电子邮件:mit.edu 的 sbrichards

关于java - AlarmManager 从不在 AlarmReceiver/BroadcastReceiver 中调用 onReceive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733736/

相关文章:

java - tcpmon Java Axis2 客户端 Web 服务响应被重定向

java - 将 apache.commons.configuration 文件更改为 pythonable configparser 文件

android - 无法解析 files.get 和 .getRequestFactory

android - 应用程序打开时警报管理器不断触发

android - 检测 WIFI BSSID 变化

android - 如何从上下文中获取 View ?

java - 使用更严格的访问修饰符覆盖方法

android - 由于更新限制错误,无法将图像发布到 Twitter

java - 在 Android 上使用 root 调用 'ls' 不会返回任何内容,应用程序挂起

java - ProgressBar propertychangeListener 在这种情况下不会更新