根据一天中的时间更改片段的内容

时间:2017-08-03 07:27:34

标签: android android-fragments

我的情况是ViewPager有6 Fragments

我希望在早上时间显示前3 Fragments,即ViewPager上午0:00至上午11:59。

我希望在下午时间显示接下来的3 Fragments,即从中午12:00到下午23:59

目前,我正在检查onResume()的{​​{1}}中的时间并更新Activity

这是我的代码:

ViewPager's Adapter

我的实施有两个问题

  1. 如果应用程序在上午11:59开放并且在前台直到 中午12:00 @Override protected void onResume() { super.onResume(); globalCalendar = Calendar.getInstance(); timeOfDay = globalCalendar.get(Calendar.HOUR_OF_DAY); if (timeOfDay >= 0 && timeOfDay < 12) { layout.setBackground(getResources().getDrawable(R.drawable.gradient_morning_background)); mSectionsPagerAdapter.notifyDataSetChanged(); mViewPager.destroyDrawingCache(); } else { layout.setBackground(getResources().getDrawable(R.drawable.gradient_background)); mSectionsPagerAdapter.notifyDataSetChanged(); mViewPager.destroyDrawingCache(); } } 不会更新。

  2. 如果我接受申请     到后台,ViewPager正在刷新     每次。

  3. 如何在用户打开应用程序时更新ViewPager's Adapter一次并加载相关的ViewPager

3 个答案:

答案 0 :(得分:2)

根据您的要求,当您前往背景并前往地面时,您的应用程序可以正常运行。

所以问题是前景senario,所以我们可以使用Handler。

我只是在这里给出提示,如果你这样做,它会正常工作。

private int mSession = -1;

private Handler mHandler = new Handler();

@Override
protected void onResume() {
    super.onResume();

    update();

    Calendar calendar = Calendar.getInstance();

    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    if (((hours == 11) || (hours == 23)) && min > 45) {
        // this is just we are starting if the current time is 11: 45 or 23:45 or more
        // We ar beliving that user may be in this screen max 15 mins
        int currentSecond = calendar.get(Calendar.SECOND);

        mHandler.postDelayed(mRunnable, (60 - currentSecond) * 1000);
    }
}

private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        update();

        mHandler.postDelayed(this, 60 * 1000);
    }
};

private void update() {
    globalCalendar = Calendar.getInstance();
    timeOfDay = globalCalendar.get(Calendar.HOUR_OF_DAY);

    if (timeOfDay >= 0 && timeOfDay < 12) {

        if (mSession != 1) {
            updateFirstHalf();
        }
        mSession = 1;
    } else {

        if (mSession != 2) {
            updateSecondHalf();
        }
        mSession = 2;
    }
}

@Override
public void onPause() {
    super.onPause();

    mHandler.removeCallbacks(mRunnable);
}

private void updateFirstHalf() {
    layout.setBackground(getResources().getDrawable(R.drawable.gradient_morning_background));
    mSectionsPagerAdapter.notifyDataSetChanged();
    mViewPager.destroyDrawingCache();
}

private void updateSecondHalf() {
    layout.setBackground(getResources().getDrawable(R.drawable.gradient_background));
    mSectionsPagerAdapter.notifyDataSetChanged();
    mViewPager.destroyDrawingCache();
}

答案 1 :(得分:0)

使用FirebaseJobDispatcherBroadcastReceiver并在注册事件中显示您的用户界面应更新的时间。

在您的活动或片段注册广播接收器中并相应地更新您的UI。

Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,12);
cal.set(Calendar.MINUTE,00);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(context, Notify.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);

在你的活动中

class MyActivity extends Activity{
  //Register and unregister your receiver in onResume and onPause
  class Notify extends BroadcastReceiver{
    //Implement OnReceive
  }
}

答案 2 :(得分:0)

这是通过闹钟管理器注册广播的方式....并从广播中完成其余的逻辑

public void registerBroadcastWithAM() {

        Intent intent = new Intent(YourActivity.this, YourBroadcastReceiver.class);


       AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Calendar firstTime = Calendar.getInstance();
        Calendar secondTurn = Calendar.getInstance();

        // set times
        firstTime .set(Calendar.HOUR_OF_DAY, 11);
        firstTime .set(Calendar.MINUTE, 59);

        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        secondTime.set(Calendar.HOUR_OF_DAY, 23);
        secondTime.set(Calendar.MINUTE, 59);
        PendingIntent alarmIntent2 = PendingIntent.getBroadcast(context, REQUEST_CODE2, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        alarmMgr.cancel(alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, secondTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent2);

    }