0

センサーの状態に基づいてSMSを送信するアプリを開発しています。

SMS は、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

}
}
4

1 に答える 1

0

私が同じ問題を抱えていたとき、私は場所の値を格納するパブリック変数を作成し、関数を呼び出す代わりに SMS 送信クラスでその変数を使用しました。申し訳ありませんが、私も正確な問題が何であるかわかりません.それが機能していたので、他に何も試しませんでした.

于 2013-03-19T08:01:24.717 に答える