ANDROID:每五分钟调用一次方法

时间:2014-04-18 16:35:20

标签: android alarmmanager android-pendingintent

如何每五分钟运行一次方法检查()? 我想每隔五分钟检查一下是否有新数据,如果是新数据,则显示通知

方法:

private void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    long when = System.currentTimeMillis();         // notification time
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, when, AlarmManager.INTERVAL_FIFTEEN_MINUTES), pendingIntent);
}

public boolean isOnline(){
      ConnectivityManager cm = (ConnectivityManager)
        getSystemService(CONNECTIVITY_SERVICE);
      NetworkInfo netinfo = cm.getActiveNetworkInfo();
      if(netinfo ==null || !netinfo.isConnectedOrConnecting())
          return false;

      return true;
}

public void check(){
    if(isOnline())
    {
        //show notification if we have new data
    }
}

0 个答案:

没有答案