在 Android 到达某个位置后发送短信
Sending Sms in Android on reaching a Location
我绝对是 android.In 这个代码的初学者,我想在到达特定位置时发送短信。如果我将 SMS 管理器放在 onlocationchange()function.So 之外,我的应用程序会崩溃,如果我没有离开 10m distance.I 只是想让它只发送一条短信,它就会发送连续的短信。请帮忙!!
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();
}
}
您好删除 Activity..
的 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();
}
}
您需要保留一个标志(布尔值)来指示短信是否已发送。
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();
}
}
我绝对是 android.In 这个代码的初学者,我想在到达特定位置时发送短信。如果我将 SMS 管理器放在 onlocationchange()function.So 之外,我的应用程序会崩溃,如果我没有离开 10m distance.I 只是想让它只发送一条短信,它就会发送连续的短信。请帮忙!!
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();
}
}
您好删除 Activity..
的 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();
}
}
您需要保留一个标志(布尔值)来指示短信是否已发送。
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();
}
}