我是android开发的初学者。我正在尝试开发一种计步器应用程序,即使应用程序关闭也会计算步数。我正在使用服务但仍然每当我按下后退按钮所有步骤计数重置。我该怎么办?提前谢谢。
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
steps=(TextView) findViewById(R.id.step_value);
distance=(TextView) findViewById(R.id.distance_value);
skbar=(SeekBar) findViewById(R.id.seekBar1);
tsen=(TextView) findViewById(R.id.textView1);
calories=(TextView) findViewById(R.id.calories_value);
sdf=new SimpleDateFormat("HH:mm:ss");
try {
date= sdf.parse(reset_time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
current=System.currentTimeMillis();
skbar.setProgress(10);
skbar.setOnSeekBarChangeListener(onSeekBarListener);
threshold=10;
tsen.setTag(String.valueOf(threshold));
prevy=0;
presenty=0;
step=0;
accelaration=0.00f;
enableAccelerometer();
if(current==set)
{
steps.setText(0);
}
}
public void enableAccelerometer(){
sensormanager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensormanager.registerListener(sensorEventListener, sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),sensormanager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener sensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
presenty=y;
if(Math.abs(presenty-prevy)>threshold)
{
step++;
steps.setText(String.valueOf(step));
x=step/3;
miles=x*(float)0.001;
distance.setText(String.valueOf(miles));
}
prevy=y;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
private OnSeekBarChangeListener onSeekBarListener=new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
threshold=skbar.getProgress();
tsen.setText(String.valueOf(threshold));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void start() {
// mContext is defined upper in code, I think it is not necessary to explain what is it
Intent i=new Intent(this,Myservice.class);
mContext.bindService(i, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(i);
}
public void stop() {
mContext.stopService(new Intent(mContext, Myservice.class));
mContext.unbindService(mServerConn);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.reset:
step=0;
steps.setText(String.valueOf(step));
}
return super.onOptionsItemSelected(item);
}
这是我的Myservice类
public class Myservice extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,"Pedometer running",Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this,"Pedometer Stopped",Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)