2016-07-03 62 views
0

我是一名學生工程師,目前我正在開發一款應用程序。我已經爲秒錶寫了一些代碼,但是有人可以幫我嗎?我想建立一個秒錶,當接近傳感器不活動時開始,並在接近傳感器處於活動狀態時停止。這是我已經有的:秒錶Android應用程序,從接近傳感器開始

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) { 


    } 
} 

回答