android 具有自动更新功能的警报 expandableListView
android Alarm expandableListView With Automatic Update
大家好,我有一个可扩展的列表视图,上面有一堆项目,当点击一个项目时,将为该项目时间设置一个警报,但因为它的警报太多,我不能告诉我的客户去改变你的警报自己然后我写了一个数据库,其中包含每个项目的更新代码,当更新代码(来自服务器)不等于数据库时,一个警报将取消并设置一个新警报但它不起作用并且它还将取消所有列表警报也没有错误我不知道为什么这是我的代码 getChildView for expandableListView adapter(在更新代码更改之前工作)
@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;
}
active code为报警激活码
这是 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
如果还有什么需要请告诉我
我会制作一个自定义对象并将其作为标签 (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;
}
然后您可以将点击处理程序添加到您的列表中 activity
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
}
}
});
大家好,我有一个可扩展的列表视图,上面有一堆项目,当点击一个项目时,将为该项目时间设置一个警报,但因为它的警报太多,我不能告诉我的客户去改变你的警报自己然后我写了一个数据库,其中包含每个项目的更新代码,当更新代码(来自服务器)不等于数据库时,一个警报将取消并设置一个新警报但它不起作用并且它还将取消所有列表警报也没有错误我不知道为什么这是我的代码 getChildView for expandableListView adapter(在更新代码更改之前工作)
@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;
}
active code为报警激活码 这是 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
如果还有什么需要请告诉我
我会制作一个自定义对象并将其作为标签 (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;
}
然后您可以将点击处理程序添加到您的列表中 activity
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
}
}
});