警报未开火

时间:2014-08-07 09:46:09

标签: android alarmmanager

我正在设置警报但由于某种原因它们不再触发(即没有调用AlarmReceiver类)。

我用来设置闹钟的代码是:

public class SetMealTimersActivity extends Activity {
    private static final int FLAG_UPDATE_CURRENT = 134217728;
    PendingIntent pi, mealPI;
    AlarmManager am;

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_meal_timers);
        am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));

        // Get current date
        Calendar cur_cal = new GregorianCalendar();
        cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current
                                                            // time and date for
                                                            // this calendar

        // Set date for alarm from current date
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
        cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

        long glbMealId = ((MealTimerApplication) this.getApplication())
                .getMealId();
        // Retrieve meal and store meal_ready_time
        meal meal = new TableControllerMeal(this).readSingleRecord(glbMealId);
        String meal_desc = meal.meal_desc;
        int meal_ready_time = meal.meal_ready_time;
        int meal_reminders_flag = meal.meal_reminders_flag;

        // Set alarms for each item in meal plan
        // Retrieve the meal plan for the meal
        List<meal_plan> meal_plan = new TableControllerMealPlan(this)
                .read(glbMealId);

        if (meal_plan.size() > 0) {
            int idx = 0;
            for (meal_plan obj : meal_plan) {

                long id = obj.id;
                String mealItemDesc = obj.meal_plan_item_desc;
                int mealItemStartTime = obj.meal_plan_item_start_time;
                int mealItemDuration = obj.meal_plan_item_duration;

                int startHours = mealItemStartTime / 60;
                int startMinutes = mealItemStartTime % 60;

                cal.set(Calendar.HOUR_OF_DAY, startHours);
                cal.set(Calendar.MINUTE, startMinutes);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                Intent alarmIntent1 = new Intent(this, AlarmReceiver.class);
                alarmIntent1.putExtra("meal_item_desc", mealItemDesc);
                alarmIntent1.putExtra("meal_id", glbMealId);
                alarmIntent1.setAction(Long.toString(id)); // Makes intent unique to prevent one notification overwriting another 
                idx++;
                pi = PendingIntent.getBroadcast(this, idx, alarmIntent1,
                        FLAG_UPDATE_CURRENT);
                am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                if (Utilities.getAPIVerison()<4.4){
                    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
                } else {
                    am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
                }

            }
        }
        //Create notification to indicate that Alarms have been set 
        PendingIntent contentIntent;
        contentIntent = PendingIntent.getActivity(SetMealTimersActivity.this,  0,
                new Intent(SetMealTimersActivity.this, MainActivity.class), 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(SetMealTimersActivity.this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Meal timer notification")
                .setContentText("Reminders Pending");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(null, 0);
        mBuilder.setAutoCancel(true);
        Notification myNotification = mBuilder.build();
        NotificationManager mNotificationManager =
            (NotificationManager) SetMealTimersActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(111, myNotification);
        Toast.makeText(this, "Reminders Set", Toast.LENGTH_SHORT).show();
        finish();
    }

我在AndroidManifest中声明了闹钟接收器:

<receiver android:name="com.ian.mealtimer.AlarmReceiver" >
</receiver>

...这里是AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String mealItem = intent.getStringExtra("meal_item_desc");
        int actionId = Integer.parseInt(intent.getAction());

        //If this is the final notification reminder for this meal then cancel the "Reminders Pending" notification
        String mealReadyNotification = context.getResources().getString(R.string.meal_ready_notification);
        if (mealItem.contains(mealReadyNotification)) {
            NotificationManager mNotificationManager1 =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                //First cancel the original Quick Timer notification
                mNotificationManager1.cancel(111);
        }

        //Get user preferences
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        Boolean vibrate = sharedPrefs.getBoolean("notifications_new_message_vibrate", false);
        String notifSound = sharedPrefs.getString("notifications_new_message_ringtone", "");

        Toast.makeText(context, "Meal Timer: " + mealItem, Toast.LENGTH_LONG).show();

        PendingIntent contentIntent;
        //Take the user to the MealItemActivity after selecting the notification
        contentIntent = PendingIntent.getActivity(context,  actionId,
                    new Intent(context, MealItemActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Meal timer notification")
                .setContentText(mealItem);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(null, 0);
        mBuilder.setSound(Uri.parse(notifSound));
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(actionId, mBuilder.build());

        // Vibrate the mobile phone if option is set in preferences
        if (vibrate) {
            Vibrator vibrator = (Vibrator) context
                      .getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(2000);
        }

    }

}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

卫生署!我刚发现问题 - 这行应该是cal.set,而不是cal.add:

cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));