0

I am developing a app which will send the sms based on the sensor condition.

Sms consists of gps location as well as the sensor value.

When I try it in emulator gps values became null and when I try it in mobile phone it showed an error. (application stopped like that)

Here is my code:

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

}
}
4

1 回答 1

0

当我遇到同样的问题时,我做了一个公共变量来存储我的位置值,并在 SMS 发送类中使用该变量而不是调用该函数。对不起,我也不知道确切的问题是什么。因为它正在工作,所以我没有尝试其他任何东西。

于 2013-03-19T08:01:24.717 回答