从意图中获得额外收益(BaseAdapter => BroadcastReceiver)

时间:2016-12-06 02:40:11

标签: android android-intent broadcastreceiver alarmmanager android-pendingintent

我正在使用闹钟应用。我想添加一个功能,当您点击特定区域时启用和禁用闹钟(所有这些都应该在listView BaseAdapter中发生)。我在正确的时间使用下面的代码和BroadcastReceiver触发器。但!!!我无法从额外的意图中获取数据:我得到默认额外的。

设置闹钟的功能:

public void setAlarm(int dayOfWeek, int hour, int minute, int position, int y) {
        // Add this day of the week line to your existing code
        Log.e("Point_1","Position " + position);
        Calendar calendar = Calendar.getInstance();

        Date previoudTime = calendar.getTime();
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent1 = new Intent(context, MyReceiver_Alarm.class);
        intent1.putExtra("Size_ABC", y);
        intent1.putExtra("key", position);
        //Log.e("Point_1", "Compare1 " + calendar.getTime());
        Log.e("Point_1", "Compare2 " + previoudTime);
        Log.e("Point_1", "Compare " + calendar.getTime().compareTo(previoudTime));
        if(calendar.getTime().compareTo(previoudTime) < 0) {

            int a = calendar.get(Calendar.WEEK_OF_MONTH);
            calendar.set(Calendar.WEEK_OF_MONTH,a + 1);
            //Log.e("Point_1", "Less " + calendar.getTime());
        }
        Long alarmTime = calendar.getTimeInMillis();

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), position , intent1, 0);
        //Also change the time to 24 hours.
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
        Log.e("Point_1", "Time is " + calendar.getTime());
    } 

这就是我获得额外的方式

Log.e("Point_1","Position_intent " + intent.getIntExtra("key",178989800));
猜猜,我得到的是什么号码?是的,正确的178989800。 如何让它正常工作?

谢谢。

1 个答案:

答案 0 :(得分:1)

而不是

PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 
         position, intent1, 0);

DO

PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
        position, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

添加PendingIntent.FLAG_UPDATE_CURRENT可确保您的&#34;额外内容&#34;被添加到PendingIntent