AlarmManager从未在AlarmReceiver /广播接收器调用onRecieve [英] AlarmManager never calling onRecieve in AlarmReceiver/BroadcastReceiver

查看:665
本文介绍了AlarmManager从未在AlarmReceiver /广播接收器调用onRecieve的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我知道有多个几十个问题在那里,就像这一个,但我用尽了一切我能想到的,仍然不能让我AlarmReceiver类的方法的onReceive火。也许我不知道我在做什么,也许我只是需要另一套的眼睛。做任何事情伸出的不对的实现?

这一切是应该做的是等待的一段时间(preferably 6天),然后弹出一个通知。 (你能相信没有一个内置的系统呢?人的crontab!?)

MyActivity和BootReceiver都设置了必要的条件下报警。 AlarmService踢出的通知。而AlarmReceiver是的应该的捕捉报警并揭开序幕AlarmService,但它从来没有抓到的广播,也不会不管我做什么。

哦,我已经在我的Droid X,2.3.4测试。项目正在兴建对API 8。

P.S。这其中大部分是改编自的http://android-in-practice.google$c$c.com/svn/trunk/ch02/DealDroidWithService/

非常感谢您的时间!

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

公共类MyActivity扩展活动实现SensorEventListener {    私人无效setupAlarm(){
        Log.i(TAG,设置报警......);
        AlarmManager alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,1,新意图(上下文,AlarmReceiver.class),0);        //从preFS报警触发时间
        Log.i(TAG,充分利用preFS报警触发的时间......);
        共享preferences米preFS2 = preferenceManager.getDefaultShared preferences(背景);
        长触发= SocUtil.getLongFrom preFS(M preFS2,AlarmConst preFS_TRIGGER);
        Log.i(TAG,触发从preFS:+触发+(+新的日期(触发)的ToString()+));        //如果报警触发没有设置
        如果(触发==新龙(-1).longValue()){
            //设置它
            触发=新的日期()的getTime()+ NOTIFY_DELAY_MILLIS。
            SocUtil.saveLongTo preFS(M preFS2,AlarmConst preFS_TRIGGER,触发);
            Log.i(TAG,触发改为:+触发+(+新的日期(触发)的ToString()+));            //和进度报警
            alarmMgr.set(AlarmManager.RTC,触发器的PendingIntent);
            Log.i(TAG,报警计划);
        }
        //如果它已经被设置
        其他{
            //没有什么安排。 BootReceiver需要重新启动后,它重新安排照顾
        }
    }}

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

公共类AlarmService扩展IntentService {   公共AlarmService(){
      超级(AlarmService);
   }   @覆盖
   公共无效onHandleIntent(意向意图){
      Log.i(AlarmConst.TAGAlarmService调用。);
      this.sendNotification(本);
   }   私人无效sendNotification时(上下文的背景下){
      Log.i(AlarmConst.TAG发送通知...);
      意图notificationIntent =新意图(背景下,Splash.class);
      的PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);      NotificationManager notificationMgr =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
      通知通知=新的通知(R.drawable.icon,测试1,System.currentTimeMillis的());
      notification.setLatestEventInfo(上下文中,Test2的,Test3的,contentIntent);
      notificationMgr.notify(0,通知);
   }
}

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

公共类AlarmReceiver扩展广播接收器{   //的onReceive必须非常快速,不会阻止,所以它只是触发了一个服务
   @覆盖
   公共无效的onReceive(上下文的背景下,意图意图){
      Log.i(AlarmConst.TAGAlarmReceiver调用,在背景开始AlarmService。);
      context.startService(新意图(上下文,AlarmService.class));
   }
}

------------ ------------ BootReceiver.java
(恢复抹报警,因为我的东西与操作系统安排不够重要,通过重新启动-_-留下来)

公共类BootReceiver扩展广播接收器{   @覆盖
   公共无效的onReceive(上下文的背景下,意图意图){
      Log.i(AlarmConst.TAG,BootReceiver调用,配置AlarmManager ......);
      Log.i(AlarmConst.TAG,设置报警......);
      AlarmManager alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,1,新意图(上下文,AlarmReceiver.class),0);      //从preFS报警触发时间
      Log.i(AlarmConst.TAG,从preFS获取报警触发的时间......);
      共享preferences米preFS2 = preferenceManager.getDefaultShared preferences(背景);
      长触发= SocUtil.getLongFrom preFS(M preFS2,AlarmConst preFS_TRIGGER);
      Log.i(AlarmConst.TAG,触发从preFS:+触发+(+新的日期(触发)的ToString()+));      //如果触发存在于preFS
      如果(触发!=新龙(-1).longValue()){
          alarmMgr.set(AlarmManager.RTC,触发器的PendingIntent);
          Log.i(AlarmConst.TAG,报警计划);
      }
   }
}

------------ ------------清单

<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>    <活动
        机器人:MyActivityNAME =
        机器人:标签=@字符串/ APP_NAME>
    < /活性GT;<接收机器人:名字=com.domain.app.BootReceiver>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
    &所述; /意图滤光器>
< /接收器><接收机器人:名字=com.domain.app.AlarmReceiver>< /接收器>    <服务机器人:名字=com.domain.app.AlarmService>< /服务>


解决方案

我用甚至没有使用广播接收器解决了这个。教程和文章中的每一个我读到有关如何执行通知警报(这是很多)表示,使用广播接收器,但显然我不明白的东西,或者说是废话负荷。

现在我只是有 AlarmManager 设定闹铃与意图直接进入到一个新的活动我创建的。我还是用 BootReceiver 在重新启动后的报警复位。

这让工作的通知在应用程序内,外的应用程序,在应用过程中死亡,在重新启动后。

由于其他评论者的时间。

Ok, so I know there's multiple dozens of questions out there just like this one, but I've tried everything I can think of and still cannot get my AlarmReceiver class' onReceive method to fire. Maybe I have no idea what I'm doing, maybe I just need another set of eyes. Does anything stick out as wrong with this implementation?

All this is supposed to do is wait a certain period of time (preferably 6 days) and then pop up a notification. (Can you believe there isn't a built in system for this? crontab anyone!?)

MyActivity and BootReceiver both set up an alarm under the necessary conditions. AlarmService kicks out a notification. And AlarmReceiver is supposed to catch the alarm and kick off AlarmService, but it has never caught that broadcast, and won't no matter what I do.

Oh, and I've been testing on my Droid X, 2.3.4. Project being built against API 8.

P.S. Most of this has been adapted from http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/

Thanks a lot for your time!

------------ 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 ------------ (to restore wiped alarms, because stuff I schedule with the OS isn't important enough to stick around through a reboot -_-)

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.");
      }
   }
}

------------ Manifest ------------

<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>

解决方案

I solved this by not even using a BroadcastReceiver. Every single one of the tutorials and posts I read about how to do notification alarms (and that was A LOT) said to use a BroadcastReceiver, but apparently I don't understand something, or that's a load of crap.

Now I just have the AlarmManager set an alarm with an Intent that goes directly to a new Activity I created. I still use the BootReceiver to reset that alarm after a reboot.

This lets the notification work in-app, out-of-app, with the app process killed, and after a reboot.

Thanks to the other commenters for your time.

这篇关于AlarmManager从未在AlarmReceiver /广播接收器调用onRecieve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆