android报警expandableListView自动更新

时间:2018-01-09 01:52:09

标签: android broadcastreceiver alarmmanager expandablelistview

大家好我有一个可扩展的列表视图,当项目点击一个项目时,项目时间设置了一堆项目,但因为它太多的警报,我不能告诉我的客户改变你的自己的警报然后我写了一个数据库,其中包含每个项目的更新代码,当更新代码(来自服务器)不等于数据库时,一个警报将取消并设置一个新警报但它不起作用并且它将取消所有列表警报没有错误我也不知道为什么这是我的代码getChildView for expandableListView适配器(在更新代码更改之前工作)

 @Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    ChildTour child =  getChild(groupPosition,
            childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.ex_list_tours_child, null);


    }

    CheckAlarms(context,groupPosition,childPosition,dbHelper);

    return convertView;

}

活动代码是警报活动代码 这是CheckAlarms方法

public void CheckAlarms(Context context, int groupPostion, int childPostion, exToursDbHelper dbHelper){
    ChildTour child =  getChild(groupPostion,
            childPostion);
    Intent intent = new Intent(context,AlarmReceiver.class);
    PendingIntent pendingIntent =PendingIntent.getBroadcast(context,child.getTourId(),intent,0);
    int dbUpdateCode=  dbHelper.getUpdateCode(child.getTourId());
    if (dbUpdateCode != child.getUpdateCode() && dbHelper.getActive(child.getTourId()) == 1 ){
        AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        manager.cancel(pendingIntent);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, child.getHour());
        calendar.set(Calendar.MINUTE, child.getMin());
        intent.putExtra("prize",child.getPrize());
        intent.putExtra("gameMode",child.getGameMode());
        intent.putExtra("time",child.getTime());
        intent.putExtra("tourId",child.getTourId());
        intent.putExtra("siteurl",child.getmSiteUrl());
        time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
        if(System.currentTimeMillis()>time)
        {
            if (calendar.AM_PM == 0)
                time = time + (1000*60*60*12);
            else
                time = time + (1000*60*60*24);
        }
        manager.set(AlarmManager.RTC_WAKEUP,time, pendingIntent);
        dbHelper.UpdateCode(child.getTourId(),child);
        Log.i("database","alarm with Id " +child.getTourId()+"is Updated");
    }
    Log.i("database","updateCode : "+child.getUpdateCode()+" databaseUpdate Code: "+dbUpdateCode);
}

我想知道为什么不工作,如果感谢你们,那么它的工作是标准方式吗?

这是日志

    01-09 05:33:33.482 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:33:33.502 23779-23779/app.mma.introsliderproject I/database: updateCode : 4 databaseUpdate Code: 4
01-09 05:33:33.522 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:33:33.532 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:33:33.552 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:33:33.562 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:33:33.582 23779-23779/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1

并在更改更新代码后

    01-09 05:34:42.462 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:34:42.482 25303-25303/app.mma.introsliderproject I/database: alarm with Id 2is Updated
01-09 05:34:42.482 25303-25303/app.mma.introsliderproject I/database: updateCode : 5 databaseUpdate Code: 4
01-09 05:34:42.492 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:34:42.512 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:34:42.522 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:34:42.542 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1
01-09 05:34:42.552 25303-25303/app.mma.introsliderproject I/database: updateCode : 1 databaseUpdate Code: 1

如果需要其他任何东西告诉我plz

1 个答案:

答案 0 :(得分:0)

我会制作一个自定义对象,并将其作为标记(getTag)添加到列表中的视图中,以便从那里跟踪状态,这样一来,如果需要设置取消设置,就可以充分观察点击发出警报。

public class AlarmState = {
    public boolean isOn = false;
    public Date changeDate;
}

public View getChildView(int groupPosition, int childPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {
    ChildTour child =  getChild(groupPosition,
        childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.ex_list_tours_child, null);
        AlarmState alarm = new AlarmState;
        alarm.isOn = true;
        alarm.changeDate = new Date();
        convertView.setTag(alarm);

    }

    CheckAlarms(context,groupPosition,childPosition,dbHelper);

    return convertView;

}

然后您可以在列表活动中添加点击处理程序

ListView list = (ListView)findViewById(R.id.listView1);

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            AlarmState alarm = (AlarmState)view.getTag();
            if (alarm.isOn) {
              // send braodcast to turn it off
            else {
              // send braodcast to turn it on
            }
        }
    });