以特定间隔调用函数

时间:2014-12-21 12:11:47

标签: android mysql sql android-activity

我正在创建一个Android应用程序,它显示了具有用户指定年龄的数据库中的男性和女性列表。我差点设法编写代码 以下是我所做的代码

public class UserList{
private ArrayList<String> maleList;
private ArrayList<String> femaleList;}  //getter setter for Arraylists 


 // method to get data
 public UserList getUserLists (Context context, String age){
public UserList userList = new UserList();
public ArrayList<String> maleList = new ArrayList<String>();
public ArrayList<String> femaleList = new ArrayList<String>();
db = dbhelper.getWritableDatabase();
Cursor cursor = db.rawQuery(
            "select * from USER_INFO where AGE = ?", new String[]{age});
if(cursor != null)
{
    if(cursor.moveToFirst())
    {
        do{
              String user_id = cursor.getString(cursor.getColumnIndex("USER_NAME"));
              String gender = cursor.getString(cursor.getColumnIndex("GENDER"));
              if(gender.equalsIgnorease("MALE"))
                  maleList.add(user_id);
              else
                  femaleList.add(user_id);
          }while(cursor.moveToNext());
          cursor.close();
    }
}
userList.setMaleList(maleList);
userList.setFemaleList(femaleList);
return userList;
 }

但现在我需要调用此函数,以便在每隔5分钟之后的某些时间间隔内将数据从数据库传输到光标。请帮我做一下这个年龄段的年龄变化。

1 个答案:

答案 0 :(得分:1)

您应该实施广播接收器并设置定期安排的闹钟。 这里有一个关于如何做到这一点的简单教程:alarm-manager-example