AlarmManager:Multiple PendingIntent,first requestCode始终为0

时间:2014-03-06 16:54:30

标签: android alarmmanager android-pendingintent

在我的应用中,我在片段中放了4个复选框和一个按钮。当我按下按钮时,它会根据所选的复选框

设置警报

复选框和其他所有内容似乎都有效,但 requestCode 存在问题,然后输出,这里有一些操作示例:

(输出是按照代码每5秒发生一次的通知)

如果我只检查其中一个:

错误,reqCode:0

如果我检查前两个:

错误,reqCode:0

警报1

如果我全部检查:

错误,reqCode:0

警报1

警报2

警报1


正如你所看到的那样,第一个警报始终是requestCode = 0但这对我来说真的没有意义..如果你能帮助我解决这个问题,我将不胜感激。谢谢你有愉快的一天


以下是代码:

AlarmReceiver.java

public class AlarmReceiver extends WakefulBroadcastReceiver {

private AlarmManager alarmMgr;

private PendingIntent alarmIntent = null, alarmIntent2 = null,
        alarmIntent3 = null, alarmIntent4 = null;

@Override
public void onReceive(Context context, Intent intent) {

    Intent service = new Intent(context, AlarmSchedulingService.class);

    service.putExtra("requestCode", intent.getExtras()
            .getInt("requestCode"));

    startWakefulService(context, service);

}

public void setAlarm(Context context) {

    alarmMgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

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

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    if (/* Checkbox1.. */) {
        alarmIntent = PendingIntent.getBroadcast(context, 1,
                intent, 0);
        intent.putExtra("requestCode", 1);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent);

    }

    if (/* Checkbox2.. */) {
        alarmIntent2 = PendingIntent.getBroadcast(context,
                2, intent, 0);
        intent.putExtra("requestCode", 2);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent2);
    }
    if (/* Checkbox3.. */) {
        alarmIntent3 = PendingIntent.getBroadcast(context,
                3, intent, 0);
        intent.putExtra("requestCode", 3);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent3);
    }

    if (/* Checkbox4.. */) {
        alarmIntent4 = PendingIntent.getBroadcast(context,
                4, intent, 0);
        intent.putExtra("requestCode", 4);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent4);
    }

}

public void cancelAlarm(Context context) {

    if (alarmMgr != null) 
    {
        if (alarmIntent != null) {
            alarmMgr.cancel(alarmIntent);
        }
        if (alarmIntent2 != null) {
            alarmMgr.cancel(alarmIntent2);
        }
        if (alarmIntent3 != null) {
            alarmMgr.cancel(alarmIntent3);
        }
        if (alarmIntent4 != null) {
            alarmMgr.cancel(alarmIntent4);
        }
    }

}

}

AlarmSchedulingService.java

public class AlarmSchedulingService extends IntentService {
public AlarmSchedulingService() {
    super("AlarmSchedulingService");
}

public static final int NOTIFICATION_ID = 1;

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

@Override
protected void onHandleIntent(Intent intent) {

    int reqCode = intent.getExtras().getInt("requestCode");

    if(reqCode == 1)
    {
        sendNotification("Alarm 1");
    }

    else if(reqCode == 2)
    {
        sendNotification("Alarm 2");
    }

    else if(reqCode == 3)
    {
        sendNotification("Alarm 3");
    }

    else if(reqCode == 4)
    {
        sendNotification("Alarm 4");
    }
    else {
        sendNotification(" Error, reqCode: " + reqCode);
    }

    AlarmReceiver.completeWakefulIntent(intent);

}

private void sendNotification(String msg) {

mNotificationManager = (NotificationManager)
           this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(getString(R.string.app_name))
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

}

0 个答案:

没有答案