2014-02-21 64 views
0

我正在研究一個與GPS協同工作並檢查用戶速度的程序。但是,不幸的是,它永遠不會運行,並且會強制關閉。 我不知道是什麼問題。我應該檢查它的互聯網連接還是.....?使用gps位置監聽器和速度

下面是我的代碼: - (有定時器一些代碼太多,但你可以忽略它)

package com.bestdiet; 


import java.util.Timer; 
import java.util.TimerTask; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.media.AudioManager; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Handler.Callback; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Training extends Activity implements LocationListener { 
    TextView text, text2, text3; 
    long starttime = 0; 
    //this posts a message to the main thread from our timertask 
    //and updates the textfield 
    final Handler h = new Handler(new Callback() { 

     @Override 
     public boolean handleMessage(Message msg) { 
      long millis = System.currentTimeMillis() - starttime; 
      int seconds = (int) (millis/1000); 
      int minutes = seconds/60; 
      seconds  = seconds % 60; 

      text.setText(String.format("%d:%02d", minutes, seconds,millis)); 
      return false; 
     } 
    }); 
    //runs without timer be reposting self 
    Handler h2 = new Handler(); 
    Runnable run = new Runnable() { 

     @Override 
     public void run() { 
      long millis = System.currentTimeMillis() - starttime; 
      int seconds = (int) (millis/1000); 
      int minutes = seconds/60; 
      seconds  = seconds % 60; 

    //  text3.setText(String.format("%d:%02d", minutes, seconds)); 

      h2.postDelayed(this, 500); 
     } 
    }; 

    //tells handler to send a message 
    class firstTask extends TimerTask { 

     @Override 
     public void run() { 
      h.sendEmptyMessage(0); 
     } 
    }; 

    //tells activity to run on ui thread 
    class secondTask extends TimerTask { 

     @Override 
     public void run() { 
      Training.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        long millis = System.currentTimeMillis() - starttime; 
        int seconds = (int) (millis/1000); 
        int minutes = seconds/60; 
        seconds  = seconds % 60; 

        // text2.setText(String.format("%d:%02d", minutes, seconds)); 
       } 
      }); 
     } 
    }; 


    Timer timer = new Timer(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_training); 

     this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
      this.setVolumeControlStream(AudioManager.STREAM_RING); 
      this.setVolumeControlStream(AudioManager.STREAM_ALARM); 
      this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION); 
      this.setVolumeControlStream(AudioManager.STREAM_SYSTEM); 
     // this.setVolumeControlStream(AudioManager.STREAM_VOICECALL); 





     text = (TextView)findViewById(R.id.textView1); 
     // text2 = (TextView)findViewById(R.id.text2); 
     // text3 = (TextView)findViewById(R.id.text3); 

     Button b = (Button)findViewById(R.id.button1); 
     b.setText("start"); 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Button b = (Button)v; 
       if(b.getText().equals("stop")){ 
        timer.cancel(); 
        timer.purge(); 
        h2.removeCallbacks(run); 
        b.setText("start"); 
       }else{ 
        starttime = System.currentTimeMillis(); 
        timer = new Timer(); 
        timer.schedule(new firstTask(), 0,500); 
        timer.schedule(new secondTask(), 0,500); 
        h2.postDelayed(run, 0); 
        b.setText("stop"); 
       } 
      } 
     }); 
     //////////////////////////////////////////////////////////// 
     Button bb=(Button)findViewById(R.id.button2); 
    } 

    @Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 
     super.onBackPressed(); 

    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     timer.cancel(); 
     timer.purge(); 
     h2.removeCallbacks(run); 
     Button b = (Button)findViewById(R.id.button); 
     b.setText("start"); 
    } 
@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    android.os.Process.killProcess(android.os.Process.myPid()); 
    super.onDestroy(); 


} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
// return super.onOptionsItemSelected(item); 
    switch(item.getItemId()) 
    { 
    case R.id.exit: 
     finish(); 
     //System.exit(0); 
     break; 
    case R.id.help: 
     Intent i=new Intent(Training.this,TrainingHelp.class); 
     startActivity(i); 
     break; 
    //case R.id.options: 
    // Intent j= new Intent(MyTimer.this,prefs.class); 
     //startActivity(j); 
     //break; 
    } 
    return false; 
} 

@Override 
public void onLocationChanged(Location loc) { 
    // TODO Auto-generated method stub 
    if(isonline()&&hasGPSEnabled()) 
    { 
     Float thespeed=loc.getSpeed(); 
     Toast.makeText(Training.this,String.valueOf(thespeed), Toast.LENGTH_LONG).show(); 
     TextView tt=(TextView)findViewById(R.id.textView3); 
     tt.setText(""+thespeed); 
     ImageView vv=(ImageView)findViewById(R.id.imageView1); 

     if(thespeed<3) 
     { 
      vv.setImageResource(R.drawable.idle); 
     } 
     if(thespeed>=3&&thespeed<10) 
     { 
      vv.setImageResource(R.drawable.walking); 
     } 
     if(thespeed>=10) 
     { 
      vv.setImageResource(R.drawable.rinnung); 
     } 
    } 
} 


private boolean hasGPSEnabled() { 
    LocationManager mlocManager = 
      (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
boolean isGPSEnabled = mlocManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 
    return isGPSEnabled; 
} 





private boolean isonline() { 
    // TODO Auto-generated method stub 
     ConnectivityManager cm = 
       (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
      if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
       return true; 
      } 
      return false; 
} 




@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 
} 
+0

您應該發佈logcat。 – EarlOfEgo

+0

請發佈您的logcat。它有助於輕鬆跟蹤錯誤 –

+0

logcat中有很多信息。 idont知道如何在其中發現問題 – virtouso

回答

2

它可能不會返回的速度。速度並不總是可用的。如果你記錄距離和時間,你可以得到速度爲d/t。您的代碼看起來不像應該產生錯誤。也許這是一個ANR。嘗試從UI線程初始化LocationListener。

// Check if device GPS is enabled. 
// Must be true to proceed 
private boolean hasGPSEnabled() { 
    LocationManager mlocManager = 
      (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    isGPSEnabled = mlocManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 
    return isGPSEnabled; 
} 
// Check if device has network connection. 
// Must be true to proceed 
public boolean isOnline() { 
    ConnectivityManager cm = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false; 
} 
+0

預先也會提供文本連接和GPS可用性。 –

+0

你能解釋一下嗎?什麼是gps連接代碼?你的意思是在try catch中編寫我的代碼?怎麼樣? – virtouso

+0

當然。我目前正在研究類似的東西,但使用位置客戶端。只需一秒鐘。 –