通知按钮什么都不做

时间:2015-05-02 11:05:29

标签: android android-intent

按钮显示在我的通知中,但是当我点击它时,它什么也没做。

广播接收器

public class AlarmReceiver extends BroadcastReceiver {

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

        String mHomeworkName = intent.getStringExtra("HOMEWORK_NAME");
        String mDueDateString = intent.getStringExtra("DUE_DATE_STRING");

        Intent oneHourRemindIntent = new Intent(context, PingService.class);
        oneHourRemindIntent.setAction(CommonConstants.ACTION_REMIND_ONE_HOUR);
        oneHourRemindIntent.putExtra("HOMEWORK_NAME", mHomeworkName);
        oneHourRemindIntent.putExtra("DUE_DATE_STRING", mDueDateString);
        oneHourRemindIntent.putExtra("HOMEWORK_INDEX",intent.getIntExtra("HOMEWORK_INDEX", 0));
        PendingIntent remindLater = PendingIntent.getService(context, 0, oneHourRemindIntent, 0);

        Intent doneIntent = new Intent(context, PingService.class);
        doneIntent.setAction(CommonConstants.ACTION_DONE);
        doneIntent.putExtra("HOMEWORK_NAME", mHomeworkName);
        doneIntent.putExtra("DUE_DATE_STRING", mDueDateString);
        doneIntent.putExtra("HOMEWORK_INDEX",intent.getIntExtra("HOMEWORK_INDEX", 0));
        PendingIntent markAsDone = PendingIntent.getService(context, 0, doneIntent, 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(mHomeworkName)
                        .setContentText("Due: " + mDueDateString)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(mHomeworkName + " is Due on " + mDueDateString))
                        .addAction(R.drawable.ic_access_alarm_black_18dp, "Snooze", remindLater)
                        .addAction (R.drawable.ic_done_black_18dp, "Done", markAsDone)
                        .setAutoCancel(true);


        Intent resultIntent = new Intent(context, HomeworkActivity.class);
        resultIntent.putExtra("HOMEWORK_INDEX",intent.getIntExtra("HOMEWORK_INDEX", 0));
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        int mNotificationId = 001;
        NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    }
}

这是我的PingService代码

public class PingService extends IntentService {

    public PingService() {

        // The super call is required. The background thread that IntentService
        // starts is labeled with the string argument you pass.
        super("com.viggi.homeworkplanner");
    }

    @Override
    protected void onHandleIntent(Intent intent){
        final String mHomeworkName = intent.getStringExtra("HOMEWORK_NAME");
        final String mDueDateString = intent.getStringExtra("DUE_DATE_STRING");
        final int mHomeworkIndex = intent.getIntExtra("HOMEWORK_INDEX", 0);
        final List<Homework> homeworkList = Homework.listAll(Homework.class);
        final Homework homework = homeworkList.get(mHomeworkIndex);

        Log.d(CommonConstants.DEBUG_TAG, CommonConstants.ACTION_REMIND_ONE_HOUR);

        NotificationManager nm = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);

        String action = intent.getAction();
        if(action.equals(CommonConstants.ACTION_DONE)) {
            homework.setmDone(1);
            homework.save();
            nm.cancel(001);
        }
        else if (action.equals(CommonConstants.ACTION_REMIND_ONE_HOUR)){
            Date reminderDate = homework.getReminderDate();
            Calendar cal = Calendar.getInstance(); // creates calendar
            cal.setTime(reminderDate); // sets calendar time/date
            cal.add(Calendar.MINUTE, 1); // adds one hour
            reminderDate = cal.getTime(); // returns new date object, one hour in the future

            homework.setReminderDate(reminderDate);
            homework.save();

            AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent(this, AlarmReceiver.class);
            alarmIntent.putExtra("HOMEWORK_NAME", mHomeworkName);
            alarmIntent.putExtra("DUE_DATE_STRING", mDueDateString);
            alarmIntent.putExtra("HOMEWORK_INDEX", mHomeworkIndex);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

            nm.cancel(001);
        }



    }
}

这是我的Activity中使用AlarmManager的代码。

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent alarmIntent = new Intent(AddHomeworkActivity.this, AlarmReceiver.class);
                alarmIntent.putExtra("HOMEWORK_NAME", homework.getHwName());
                alarmIntent.putExtra("DUE_DATE_STRING", mDueDateString);
                List<Homework> homeworkList = Homework.listAll(Homework.class);
                for (int i = 0; i < homeworkList.size(); i++){
                    Homework homeworkObject = homeworkList.get(i);
                    if (homeworkObject.getSubjName().equals(homework.getSubjName()) && homeworkObject.getHwName().equals(homework.getHwName())){
                        alarmIntent.putExtra("HOMEWORK_INDEX", i);
                    }
                }
                Calendar reminderCalendarDate = Calendar.getInstance();
                reminderCalendarDate.setTime(reminderDateTime.toDate());
                PendingIntent pendingIntent = PendingIntent.getBroadcast(AddHomeworkActivity.this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                alarmMgr.set(AlarmManager.RTC_WAKEUP, reminderCalendarDate.getTimeInMillis(), pendingIntent);

我的按钮只停留在那里,根本不起作用。

0 个答案:

没有答案