Developing a timer application,
Have 4 buttons
start--Will start the timer
stop-- will stop timer
pause--will pause timer
lap time--will calculate lap time.
when button click it s working Good.
Now i am Modify the application like timer should start when phone is connected to charger and pause when phone is disconnected from charger.
The timer is starting fine when connected to the charger,
but during discharging the stop timer is not stopping at the accurate time it will be misplaced with some other time.i.e delay in minutes and seconds.
How to make the timer pause and show correct timer when disconnected from the charger...?
MainActivity.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private static final String TAG ="Main Activity";
TextView textView;
Button start, pause, reset, lap;
long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L;
Handler handlerr;
int Seconds, Minutes, MilliSeconds;
ListView listView;
String[] ListElements = new String[]{};
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
TextView textview;
Button button;
IntentFilter intentfilter;
int deviceStatus;
String currentBatteryStatus = "Battery Info";
int batteryLevel;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textViewBatteryStatus);
intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
MainActivity.this.registerReceiver(broadcastreceiver, intentfilter);
textView = (TextView)findViewById(R.id.textView);
start = (Button)findViewById(R.id.button);
pause = (Button)findViewById(R.id.button2);
reset = (Button)findViewById(R.id.button3);
lap = (Button)findViewById(R.id.button4) ;
listView = (ListView)findViewById(R.id.listview1);
handler = new Handler() ;
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
ListElementsArrayList
);
listView.setAdapter(adapter);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
reset.setEnabled(false);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
reset.setEnabled(true);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MillisecondTime = 0L ;
StartTime = 0L ;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;
MilliSeconds = 0 ;
textView.setText("00:00:00");
ListElementsArrayList.clear();
adapter.notifyDataSetChanged();
}
});
lap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ListElementsArrayList.add(textView.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
public Runnable runnable = new Runnable() {
public void run() {
MillisecondTime = SystemClock.uptimeMillis() - StartTime;
UpdateTime = TimeBuff + MillisecondTime;
Seconds = (int) (UpdateTime / 1000);
Minutes = Seconds / 60;
Seconds = Seconds % 60;
MilliSeconds = (int) (UpdateTime % 1000);
textView.setText("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds));
handler.postDelayed(this, 0);
}
};
// Broadcasts receiver//
private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int batteryLevel=(int)(((float)level / (float)scale) * 100.0f);
if(deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING){
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
reset.setEnabled(false);
textview.setText(currentBatteryStatus+" = Charging at "+batteryLevel+" %");
}
if(deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){
textview.setText(currentBatteryStatus+" = Discharging at "+batteryLevel+" %");
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL){
textview.setText(currentBatteryStatus+"= Battery Full at "+batteryLevel+" %");
}
if(deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN){
textview.setText(currentBatteryStatus+" = Unknown at "+batteryLevel+" %");
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
reset.setEnabled(true);
textview.setText(currentBatteryStatus+" = Not Charging at "+batteryLevel+" %");
}
}
};
}
activity_main.xml
<TextView android:text="00:00:00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="50dp" android:textStyle="bold" android:textColor="#009688" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:text="Start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="41dp" android:id="@+id/button" /> <Button android:text="Pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_alignBaseline="@+id/button" android:layout_alignBottom="@+id/button" android:layout_centerHorizontal="true" /> <Button android:text="Reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button2" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/button3" /> <Button android:text="Save Lap" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:id="@+id/button4" android:layout_below="@+id/button" android:layout_centerHorizontal="true" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/button4" android:layout_centerHorizontal="true" android:layout_marginTop="12dp" android:id="@+id/listview1"/> <TextView android:id="@+id/textViewBatteryStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/button2" android:layout_below="@+id/textView" android:text="Current Battery Status" android:textAppearance="?android:attr/textAppearanceLarge" />
Guidance in editing code will be helpful.