【问题标题】:Make button visible at the start of every hour?让按钮在每小时开始时可见?
【发布时间】:2017-02-22 12:27:56
【问题描述】:

我的主要活动有一个按钮,可以打开一个表单活动,您可以在其中提交数据。提交数据后,按钮将消失。如何让它出现在一天中每个小时的开始?

例如,如果我在晚上 7:05 提交数据,则该按钮会一直消失到晚上 8:00。当我再次按下按钮提交数据时,按钮会消失,直到晚上 9:00,以此类推。

【问题讨论】:

  • 试试 jobScheduler 或 AlarmManager

标签: java android android-studio


【解决方案1】:

当您提交数据时获取当前时间戳并将其存储在 sharedPref 中。当您开始活动时检查 sharedPref 并将其与设备的当前时间进行比较。

【讨论】:

    【解决方案2】:

    尝试使用后台服务并将通知间隔设置为 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(new Intent(getBaseContext(), BackgroundService.class));

    //停止后台服务

    stopService(new Intent(getBaseContext(), BackgroundService.class));

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多