我的主要活动有一个按钮,用于在提交数据的位置打开表单活动。提交数据后,按钮将消失。我如何让它在当天每个小时的开始时出现?Make按钮在每小时的开始时都可见?
例如,如果我在晚上7:05提交数据,该按钮将消失,直到下午8:00。当我按下按钮再次提交数据时,该按钮将消失至晚上9点,依此类推。
我的主要活动有一个按钮,用于在提交数据的位置打开表单活动。提交数据后,按钮将消失。我如何让它在当天每个小时的开始时出现?Make按钮在每小时的开始时都可见?
例如,如果我在晚上7:05提交数据,该按钮将消失,直到下午8:00。当我按下按钮再次提交数据时,该按钮将消失至晚上9点,依此类推。
当您提交数据时,获取当前时间戳记并将其存储在sharedPref中。开始活动时,请检查sharedPref并将其与设备的当前时间进行比较。
尝试使用后台服务,并在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(新意图(getBaseContext(),BackgroundService.class));
//停止后台服务
stopService(新意图(getBaseContext(),BackgroundService.class));
尝试jobScheduler或AlarmManager –