开发计时器应用程序
有4个按钮
开始 - 将启动计时器
停止 - 将停止计时器
暂停 - 将暂停计时器
单圈时间 - 将计算单圈时间。
当按钮点击它时工作正常。
现在我修改应用程序,如计时器应该在手机连接到充电器时启动,并在手机与充电器断开时暂停。
连接充电器时,计时器正常启动
但在放电期间,停止计时器没有在准确的时间停止,它会被其他时间错位。即延迟分钟和秒。
如何在与充电器断开连接时使定时器暂停并显示正确的定时器...?
MainActivity.java
switch (y) {
case 0:
$('slpage').style.height = '0px';
break;
case 1:
case 2:
$('slpage').style.height = '70px';
break;
case 3:
case 4:
$('slpage').style.height = '140px';
break;
case 5:
default:
$('slpage').style.height = '210px';
}
activity_main.xml中
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+" %"); } } }; }
编辑代码的指导会很有帮助。
答案 0 :(得分:0)
如果你想捕捉Charger Connected和Disconnected事件,那么你不应该在第一时间使用BATTERY_STATUS_CHARGING
和BATTERY_STATUS_DISCHARGING
。你应该听 -
<receiver android:name=".YourPowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
因此,只有在充电器连接和断开状态下才能进行广播。
请看这里:https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED