2017-02-22 10 views
-3

В моем основном действии есть кнопка, которая открывает активность формы, в которой вы отправляете данные. После отправки данных кнопка исчезнет. Как заставить его появляться в начале каждого часа дня?Сделать кнопку видимой в начале каждого часа?

Например, если бы я представил данные в 7:05 вечера, кнопка исчезла бы до 8:00 вечера. Когда я снова нажимаю кнопку, чтобы отправить данные, кнопка исчезнет до 21:00 и так далее.

+1

попробуйте jobScheduler или AlarmManager –

ответ

0

Когда вы отправляете данные, получите текущую временную метку и сохраните ее в sharedPref. Когда вы начнете проверку активности sharedPref и сравните ее с текущим временем устройства.

0

Попробуйте использовать фоновую службу и установить интервал уведомления в 1 ч и запуска службы при нажатии кнопки мыши и после, чем все, что вы хотите сделать с этим фоном службы

BackgroundService.java 

public class BackgroundService extends Service { 

public static final int notify = 100000; //interval between two services(Here Service run every 1 Minute) 

private Timer mTimer = null; //timer handling 

Context context; 

ScheduledExecutorService scheduleTaskExecutor; 

View view; 

Handler mHandler = new Handler(); 

/** indicates how to behave if the service is killed */ 
int mStartMode; 

/** interface for clients that bind */ 
IBinder mBinder; 

/** indicates whether onRebind should be used */ 
boolean mAllowRebind; 

/** Called when the service is being created. */ 
@Override 
public void onCreate() { 
    super.onCreate(); 
    context = getApplicationContext(); 

    Toast.makeText(this, "Service is created", Toast.LENGTH_SHORT).show(); 

    if (mTimer != null) // Cancel if already existed 
     mTimer.cancel(); 
    else 
     mTimer = new Timer(); //recreate new 
    mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify); //Schedule task 
} 

/** The service is starting, due to a call to startService() */ 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return super.onStartCommand(intent, flags, startId); 

} 

@Override 
public IBinder onBind(Intent intent){ 
    return null; 
} 

/** Called when all clients have unbound with unbindService() */ 
@Override 
public boolean onUnbind(Intent intent) { 
    return mAllowRebind; 
} 

/** Called when a client is binding to the service with bindService()*/ 
@Override 
public void onRebind(Intent intent) { 

} 

/** Called when The service is no longer used and is being destroyed */ 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mTimer.cancel(); //For Cancel Timer 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
} 

//class TimeDisplay for handling task 
class TimeDisplay extends TimerTask { 
    @Override 
    public void run() { 
     // run on another thread 
     mHandler.post(new Runnable() { 
      @Override 
      public void run() { 
     // Toast.makeText(this, "Service Start",Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
} 

}

AndroidManifest.xml 
<service android:name=".BackgroundService.KametSportsEventsService" android:enabled="true" /> 

// Начало фоновой службы

StartService (новый Intent (getBaseContext(), BackgroundService.class));

// Останов Справочная служба

StopService (новый Intent (getBaseContext(), BackgroundService.class));

 Смежные вопросы

  • Нет связанных вопросов^_^