0

I am new in android viewpager. I have developed a application in which using sliding tablayout. So on every tab the particular fragment will be shown.

On first tab: I want to show user's phone battery information

In the main activity, i have registered a broadcastreceiver for battery, as:

public static BroadcastReceiver batteryInfoReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int health=intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
        int iconSmall=intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0);
        level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
        int plugged=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
        boolean present=intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
        int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
        int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
        String technology=intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
        temp=intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
        voltage=intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);


    }
};

Here is my first fragment on which i want to display battery info:

public class Tab1 extends Fragment {

//initializing child views
public TextView temp,level;
public ProgressBar batteryProgress;

public  Tab1()
{
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   // return inflater.inflate(R.layout.tab1,container,false);
    View v=inflater.inflate(R.layout.tab1,container,false);
            return v;
}

@Override
public void onViewCreated(View view,Bundle savedInstanceState) {

    level = (TextView) getActivity().findViewById(R.id.level);
    temp=(TextView) getActivity().findViewById(R.id.voltageNtemp);
    batteryProgress=(ProgressBar) getActivity().findViewById(R.id.batteryProgress);

    //setting battery info
    level.setText(String.valueOf(MainActivity.level)+"%");
    temp.setText("V: "+String.valueOf(MainActivity.voltage)+" mV ,  T: "+String.valueOf(MainActivity.temp)+" C");
    batteryProgress.setMax(100);
    batteryProgress.setProgress(MainActivity.level);

}}

This is the pager adapter class

public class Pager extends FragmentStatePagerAdapter {

//integer to count number of tabs
int tabCount;
int current;

int voltage, temp, level;

//constructor to the class
public Pager(FragmentManager fm, int tabCount)
{
    super(fm);
    //initializing tab count
    this.tabCount=tabCount;
}

@Override
public Fragment getItem(int position) {
    this.current=position;
    switch (position)
    {
        case 0:
            Tab1 t1=new Tab1();
            return t1;

        case 1:
            Tab2 t2=new Tab2();
            return t2;

        case 2:
            Tab3 t3=new Tab3();
            return t3;

        default:
            return null;
    }
}



@Override
public int getCount() {
    return tabCount;
}

And in main activity :

//initializing the tablayout
    tabLayout=(TabLayout)findViewById(R.id.tabLayout);

    //adding tabs using assTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Overview"));//set tab title
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //initializing viewpager
    viewPager=(ViewPager) findViewById(R.id.pager);

    //creating out pager adapter
    adapter=new Pager(getSupportFragmentManager(),tabLayout.getTabCount());

    //adding adapter to pager
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    //adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);

    this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

so using this code: I am successfully getting battery voltage, temperature and level at the point when the app starts.

But suppose, the phone status is currently charging and the user is using this current fragment. means, if the fragment is alive.

I want to refresh the fragment every N seconds, so the code will determine the changes in the battery level and will update the fragment data dynamically.

anyone please help

Ali
  • 3,373
  • 5
  • 42
  • 54
av development
  • 53
  • 1
  • 10
  • 1
    you can use `Handler` i suppose ; for timing operations – Tahmid Rahman Feb 25 '17 at 05:22
  • actually i dont have any idea how to use handler. so that the fragment will update dynamically – av development Feb 25 '17 at 05:27
  • you wanted to check each 1 sec right? in that case that would be a sheduled task – Tahmid Rahman Feb 25 '17 at 05:29
  • you are not getting my point. i know i have to use something like timer. but i dont know how to implement it. because here is the case of fragment.will you plz suggest me a sample code ?plz' – av development Feb 25 '17 at 05:31
  • i am sorry your question is marked as duplicate. I cannot help you by answering your question. [Check this ques](http://stackoverflow.com/q/13422818/6155248) – Tahmid Rahman Feb 25 '17 at 05:34
  • Or else you can check [this question](http://stackoverflow.com/q/20702333/6155248) – Tahmid Rahman Feb 25 '17 at 05:35
  • Use a [TimerTask](https://developer.android.com/reference/java/util/TimerTask.html). [HERE](http://stackoverflow.com/questions/10029831/how-do-you-use-a-timertask-to-run-a-thread) is a nice example: – NMP Feb 25 '17 at 07:53

0 Answers0