如何区分不同的接收报警

时间:2016-07-31 08:17:29

标签: android out-of-memory

我正在让用户选择我在数据库中插入的三个警报。用户总是必须输入第一个警报,但他可以选择是否要发出第二个或第三个警报。我可以成功发出警报。但我不知道如何区分我的AlarmReceiver类中的警报。有什么建议吗?

我在这里从数据库中获取数据:

@Override
    protected void onPostExecute(Cursor result) {
    super.onPostExecute(result);

    boolean data = result.moveToFirst();
    try {
        while (data) {
            int timeInMilliSecondsAlarm1Index = result.getColumnIndex("alarm1");
            timeInMilliSecondsAlarm1 = result.getInt(timeInMilliSecondsAlarm1Index);
            if (timeInMilliSecondsAlarm1 > 0) {
                Alarm1TimeList.add(timeInMilliSecondsAlarm1);
                countForAlarm++;
            }
            int timeInMilliAlarm2Index = result.getColumnIndex("alarm2");
            timeInMilliSecondsAlarm2 = result.getInt(timeInMilliAlarm2Index);
            if(timeInMilliSecondsAlarm2>0) {
                Alarm2TimeList.add(timeInMilliSecondsAlarm2);
                countForAlarm++;
            }
            int timeInMilliAlarm3Index = result.getColumnIndex("alarm3");
            timeInMilliSecondsAlarm3 = result.getInt(timeInMilliAlarm3Index);
            if(timeInMilliSecondsAlarm3>0) {
                Alarm3TimeList.add(timeInMilliSecondsAlarm3);
                countForAlarm++;
            }
            count++;
            data = result.moveToNext();
        }
    }catch (OutOfMemoryError ee)
    {
        Log.d("SetMyAlarm","OutOFMemory");
    }
    result.close();
    prescriptiondatabaseConnector.close();
    startMyAlarm();
}

这里我根据数据库中的数据动态设置警报:

 public void startMyAlarm() {
        AlarmManager [] alarmMgr = new AlarmManager[countForAlarm];
        for (int i = 0; i < countForAlarm; i++) {
            if (Alarm1TimeList != null) {
                if (i < Alarm1TimeList.size()) {
                    timeInMilliSecondsAlarm1 = Alarm1TimeList.get(i);
                    if (timeInMilliSecondsAlarm1 > 0) {
                        Intent intent = new Intent(this, AlarmReceiver.class);
                        alarmIntentForAlarm1 = PendingIntent.getBroadcast(this, alarmRequestCodeForAlarm1, intent, 0);
                        alarmMgr[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmMgr[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSecondsAlarm1, alarmIntentForAlarm1);
                        alarmRequestCodeListForAlarm1.add(alarmRequestCodeForAlarm1);
                        intentArrayListForAlarm1.add(alarmIntentForAlarm1);
                        alarmRequestCodeForAlarm1++;
                    }
                }
            }
            if (Alarm2TimeList != null) {
                if (i < Alarm2TimeList.size()) {
                    timeInMilliSecondsAlarm2 = Alarm2TimeList.get(i);
                    if (timeInMilliSecondsAlarm2 > 0) {
                        Intent intent = new Intent(this, AlarmReceiver.class);
                        alarmIntentForAlarm2 = PendingIntent.getBroadcast(this, alarmRequestCodeForAlarm2, intent, 0);
                        alarmMgr[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmMgr[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSecondsAlarm2, alarmIntentForAlarm2);
                        alarmRequestCodeListForAlarm2.add(alarmRequestCodeForAlarm2);
                        intentArrayListForAlarm2.add(alarmIntentForAlarm2);
                        alarmRequestCodeForAlarm2++;
                    }
                }
            }
            if (Alarm3TimeList != null) {
                if (i < Alarm3TimeList.size()) {
                    timeInMilliSecondsAlarm3 = Alarm3TimeList.get(i);
                    if (timeInMilliSecondsAlarm3 > 0) {
                        Intent intent = new Intent(this, AlarmReceiver.class);
                        alarmIntentForAlarm3 = PendingIntent.getBroadcast(this, alarmRequestCodeForAlarm3, intent, 0);
                        alarmMgr[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmMgr[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSecondsAlarm3, alarmIntentForAlarm3);
                        alarmRequestCodeListForAlarm3.add(alarmRequestCodeForAlarm3);
                        intentArrayListForAlarm3.add(alarmIntentForAlarm3);
                        alarmRequestCodeForAlarm3++;
                    }
                }
            }
        }
    }

这有效,但我不明白的是如何区分我在AlarmReceiver中发出的警报。

我试过了,但它没有用。

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        for(int i = 0; i < SetMyAlarm.intentArrayListForAlarm1.size(); i++)
        {
            if(intent.equals(SetMyAlarm.intentArrayListForAlarm1.get(i)))
            {
                Toast.makeText(context,"I was fired but i came back",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

但这可行

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
                Toast.makeText(context,"I was fired but i came back",Toast.LENGTH_SHORT).show();

}

}

1 个答案:

答案 0 :(得分:0)

这不起作用,因为{ TextView wordList = new TextView(this); wordList.setText(names.get(index)); rootview.addView(wordList); index++; } { TextView wordList = new TextView(this); wordList.setText(names.get(index)); rootview.addView(wordList); index++; } 不能以这种方式对意图起作用。一个简单的解决方案是将值传递到equalsintent

PendingIntent

然后在你的广播中:

intent.putExtra("code", 1);