2013-02-09 52 views
0

我正在開發一個應用程序,它將基於傳感器條件發送短信。無法根據傳感器值與GPS發送短信

短信由gps位置以及傳感器值組成。

當我在模擬器中嘗試它時,gps值變爲null,當我在手機中嘗試它時顯​​示錯誤。 (應用程序停止像)

這裏是我的代碼:

public class MainActivity extends Activity implements SensorEventListener , LocationListener { 

    private SensorManager sensorManager; 
    TextView xCoor; // declare X axis object 
    TextView yCoor; // declare Y axis object 
    TextView zCoor; // declare Z axis object 
    TextView aCoor; 
    public static float x; 
    public static float y; 
    public static float z; 
    String theftreport; 
    String Text; 



@Override 
    public void onCreate(Bundle savedInstanceState) 
{ 

    //for sensor 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object 
    yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object 
    zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object 

    sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE); 
     sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST); 

    //for gps 
    // super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MainActivity(); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 


@Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { } 

@Override 
public void onLocationChanged(Location loc) { 
loc.getLatitude(); 
    loc.getLongitude(); 

    Text = "My current location is: " +"Latitud =" + 
      " " + loc.getLatitude() +"Longitud = " + loc.getLongitude(); 

    // Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 


} 

@Override 
public void onSensorChanged(SensorEvent event){ 

    // check sensor type 
    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ 
    // assign direction 

     x=event.values[0]; 
     y=event.values[1]; 
     z=event.values[2]; 
     xCoor.setText("X: "+x); 
     yCoor.setText("Y: "+y); 
     zCoor.setText("Z: "+z); 



     if(x>=5) 
     { 

     theftreport=Text.toString() + "Crash rate : " + x; 

     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage("900XXXXXXX", null, theftreport, null,null);              


     Toast.makeText(MainActivity.this, "message sent", Toast.LENGTH_LONG).show(); 

     } 
             } 

} 


@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
} 


@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 

} 


@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 
} 
+0

@SList正如他的問題所述,他開發的應用程序顯然是在Android平臺上。所以你可以添加標籤,因爲你已經試圖編輯他的問題。 – Kanth 2013-02-09 13:16:44

+0

確實在清單文件中檢查gps權限? – nayab 2013-02-17 17:17:44

+0

是的,我檢查了Nadosh,但仍然得到0.0作爲價值..你現在,如果我烤麪包值工作..但當我用它來發送短信它返回0.0作爲價值.. – 2013-03-07 17:30:13

回答

0

,當我有什麼,我做的是我犯了一個公共變量來存儲我的位置值相同的問題,並使用該變量的短信發送類而不是調用該函數。 對不起,我也不知道什麼是確切的問題。因爲它正在工作,我沒有嘗試其他任何東西。