2017-03-17 30 views
2

我是一个绝对的初学者在android.In此代码我想发送短信到达特定位置。我的应用程序崩溃,如果我把短信管理器外部onlocationchange()函数。所以它发送连续的短信,如果我没有走出10米距离。我只是想让它发送只有一个短信。请帮忙!!在Android上发送短信到达一个位置

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtLat = (TextView) findViewById(R.id.textview1); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    try { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,10, this); 
    } catch (SecurityException e) 
    { 
     txtLat = (TextView) findViewById(R.id.textview1); 
     txtLat.setText("Security Exception"); 

    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    txtLat = (TextView) findViewById(R.id.textview1); 
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
    float dist1=getsignaldistance(location); 
    tx=(TextView)findViewById((R.id.textview2)); 
    tx.setText("Distance for next signal:"+Float.toString(dist1)+"m"); 
    checksendsms(location); 
} 

private void checksendsms(Location location) { 
    Location strpoint=new Location("strpoint"); 
    strpoint.setLatitude(location.getLatitude()); 
    strpoint.setLongitude(location.getLongitude()); 
    Location endpoint=new Location("endpoint"); 
    endpoint.setLatitude(10.813763); 
    endpoint.setLongitude(78.644309); 
    float dist=strpoint.distanceTo(endpoint); 
    tx=(TextView)findViewById((R.id.textview3)); 
    tx.setText("Distance between points:"+Float.toString(dist)+"m"); 
    if(dist<=10.0) 
    { 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phonenumber, null, message, null, null); 
     finish(); 
    } 

} 
+0

您需要保留一个标志(布尔值),指示是否已经发送了短信。 – Distwo

回答

0

你需要保持一个标记(布尔)指示,如果有短信已经发送。

private boolean mHasSmsBeenSent = false; // <--- HERE 

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtLat = (TextView) findViewById(R.id.textview1); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    try { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,10, this); 
    } catch (SecurityException e) 
    { 
     txtLat = (TextView) findViewById(R.id.textview1); 
     txtLat.setText("Security Exception"); 

    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    txtLat = (TextView) findViewById(R.id.textview1); 
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
    float dist1=getsignaldistance(location); 
    tx=(TextView)findViewById((R.id.textview2)); 
    tx.setText("Distance for next signal:"+Float.toString(dist1)+"m"); 
    if(!mSmsHasBeenSent) {  // <-- HERE 
     checksendsms(location); 
    } 
} 

private void checksendsms(Location location) { 
    Location strpoint=new Location("strpoint"); 
    strpoint.setLatitude(location.getLatitude()); 
    strpoint.setLongitude(location.getLongitude()); 
    Location endpoint=new Location("endpoint"); 
    endpoint.setLatitude(10.813763); 
    endpoint.setLongitude(78.644309); 
    float dist=strpoint.distanceTo(endpoint); 
    tx=(TextView)findViewById((R.id.textview3)); 
    tx.setText("Distance between points:"+Float.toString(dist)+"m"); 
    if(dist<=10.0) 
    { 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phonenumber, null, message, null, null); 
     mHasSmsBeenSent = true; // <--- HERE 
     finish(); 
    } 

} 
+0

糟糕!谢谢你解决我的问题! –

0

喜取出位置回调在活动的onDestroy ..

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtLat = (TextView) findViewById(R.id.textview1); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    try { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,10, this); 
    } catch (SecurityException e) 
    { 
     txtLat = (TextView) findViewById(R.id.textview1); 
     txtLat.setText("Security Exception"); 

    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    locationManager.removeUpdates(this); 
} 


@Override 
public void onLocationChanged(Location location) { 
    txtLat = (TextView) findViewById(R.id.textview1); 
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
    float dist1=getsignaldistance(location); 
    tx=(TextView)findViewById((R.id.textview2)); 
    tx.setText("Distance for next signal:"+Float.toString(dist1)+"m"); 
    checksendsms(location); 
} 

private void checksendsms(Location location) { 
    Location strpoint=new Location("strpoint"); 
    strpoint.setLatitude(location.getLatitude()); 
    strpoint.setLongitude(location.getLongitude()); 
    Location endpoint=new Location("endpoint"); 
    endpoint.setLatitude(10.813763); 
    endpoint.setLongitude(78.644309); 
    float dist=strpoint.distanceTo(endpoint); 
    tx=(TextView)findViewById((R.id.textview3)); 
    tx.setText("Distance between points:"+Float.toString(dist)+"m"); 
    if(dist<=10.0) 
    { 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phonenumber, null, message, null, null); 
     finish(); 
    } 

} 
+0

非常感谢您的帮助! –

+0

请投票并让这个答案正确的答案。 – RaghavPai