我是一名学生工程师,我目前正在开发一款应用。我已经为秒表写了一些代码,但有人可以帮我吗?我想建立一个秒表,它在接近传感器未激活时启动,并在接近传感器激活时停止。这就是我已经拥有的:
package com.example.michiel.myapplication2;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import static com.google.android.gms.common.api.GoogleApiClient.Builder;
public class MainActivity extends Activity implements SensorEventListener{
Button butnstart, butnreset;
TextView time;
TextView test;
long starttime = 0L;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedtime = 0L;
int t = 1;
int secs = 0;
int mins = 0;
int milliseconds = 0;
Handler handler = new Handler();
// sensor
SensorManager sm;
Sensor proxSensor;
boolean afstand;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butnstart = (Button) findViewById(R.id.start);
butnreset = (Button) findViewById(R.id.reset);
time = (TextView) findViewById(R.id.timer);
//sensor
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
test = (TextView) findViewById(R.id.test);
sm.registerListener (this,proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
butnstart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (t == 1) {
butnstart.setText("Pause");
starttime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimer, 0);
t = 0;
} else {
butnstart.setText("Start");
time.setTextColor(Color.BLUE);
timeSwapBuff += timeInMilliseconds;
handler.removeCallbacks(updateTimer);
t = 1;
}
}
});
butnreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
starttime = 0L;
timeInMilliseconds = 0L;
timeSwapBuff = 0L;
updatedtime = 0L;
t = 1;
secs = 0;
mins = 0;
milliseconds = 0;
butnstart.setText("Start");
handler.removeCallbacks(updateTimer);
time.setText("00:00:00");
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new Builder(this).addApi(AppIndex.API).build();
}
public Runnable updateTimer = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - starttime;
updatedtime = timeSwapBuff + timeInMilliseconds;
secs = (int) (updatedtime / 1000);
mins = secs / 60;
secs = secs % 60;
milliseconds = (int) (updatedtime % 1000);
time.setText("" + mins + ":" + String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
time.setTextColor(Color.RED);
handler.postDelayed(this, 0);
}
};
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.example.michiel.myapplication2/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.example.michiel.myapplication2/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
@Override
public void onSensorChanged(SensorEvent event) {
if (afstand==true){
afstand=false;
}
else afstand =true;
test.setText("detectie");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
答案 0 :(得分:0)
似乎SensorEvent必须具有值参数,以cm为单位显示到对象的距离。所以,你必须这样做smtng:
if(event.values[0] <= 2){
//do something
}
有关详细信息,请查看此页面: https://developer.android.com/guide/topics/sensors/sensors_position.html