2017-03-17 8 views
2

나는 안드로이드에서 절대 초보자입니다.이 코드에서 특정 위치에 도달 할 때 SMS를 보내려고합니다. 내 앱이 내가 10m 거리에서 나오지 않으면 계속 SMS를 보내고있다. 단지 한 SMS 만 보내고 싶다. 도와주세요!!위치에 도달하면 안드로이드에서 SMS 보내기

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

이미 sms가 전송되었는지 나타내는 플래그 (부울 값)를 유지해야합니다. – 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

안녕 당신은 SMS가있는 경우 표시 플래그 (부울)를 유지할 필요가 있습니다 .. 활동의들의 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