4

我仍然无法触发我的 AlarmReceiver 类的 onReceive 方法。这个实现有什么问题吗?

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

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

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

PS 大部分内容改编自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.");
      }
   }
}

- - - - - - 显现 - - - - - -

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

3 回答 3

3

这是我最近用来每小时发出一次通知的一些代码(这是在我的 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>

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

电子邮件: sbrichards at mit.edu

于 2012-07-31T05:57:24.993 回答
0

您必须使用意图操作注册您的 AlarmReceiver。如下所示。并且动作字符串必须与您通过 sendBroadcast() 方法广播的动作相同。

喜欢sendBroadcast(new Intent(""com.intent.action.SOMEACTION.XYZ""));

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

<intent-filter>
                <action android:name="com.intent.action.SOMEACTION.XYZ" />
            </intent-filter>
</receiver>
于 2012-07-31T06:20:41.837 回答
-3

我什至不使用BroadcastReceiver. 我读到的关于如何做通知警报的每一篇教程和帖子(那是很多)都说使用 a BroadcastReceiver,但显然我不明白什么,或者那是一堆废话。

现在我只是AlarmManager设置了一个警报,Intent它直接进入Activity我创建的新警报。重新启动后,我仍然使用BootReceiver来重置该警报。

这让通知可以在应用程序内、应用程序外、应用程序进程被终止以及重新启动后工作。

感谢其他评论者的时间。

于 2012-08-02T02:16:57.457 回答