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