It is necessary to display the battery charge on the screen, which is what I actually do, but then it should change depending on the level, but with me it prints every time the charge is under each other ...

public class MainActivity extends AppCompatActivity { private Context mContext; private TextView mTextViewInfo; private TextView mTextViewPercentage; private int mProgressStatus = 0; private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1); mTextViewInfo.setText(mTextViewInfo.getText() + "Battery Level : " + level); } }; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); mContext.registerReceiver(mBroadcastReceiver,iFilter); // Get the widgets reference from XML layout mTextViewInfo = (TextView) findViewById(R.id.tv_info); } } 
  • one
    Why line mTextViewInfo.getText() ? - pavel163
  • one
    mTextViewInfo.getText() returns the current text in the text field, accordingly it is not difficult to guess why you add text from the bottom and not replace the old one. - temq
  • why am I really taking the text))) - elik

0