Попробуйте использовать фоновую службу и установить интервал уведомления в 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));
попробуйте jobScheduler или AlarmManager –