无法使用gps根据传感器值发送短信

时间:2013-02-09 10:29:01

标签: android gps sms accelerometer sensor

我正在开发一个应用程序,它将根据传感器条件发送短信。

短信由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

}
}

1 个答案:

答案 0 :(得分:0)

当我遇到同样的问题时,我做了一个公共变量来存储我的位置值并在SMS发送类中使用该变量而不是调用该函数。 很抱歉,我也不知道究竟是什么问题。因为它工作我没有尝试其他任何事情。