我的应用是GPS追踪应用
。
应用程序在后台运行,每10秒检查一次,如果位置正在改变。如果位置发生变化,应用程序将发送带有新位置的短信。如果不是它会继续检查。
我的问题是如何使用检测到的当前位置检查最后一个位置
如何使用(如果条件)查看最后一个位置是否仍未改变
我想检查值是否相同(相同的位置),以了解设备是否移动到另一个地方或仍然在该位置
在update()。
double a=gps.getLatitude();
double b=gps.getLongitude();
@Override
public void run()
{
if ( a==Ch1 && b==Ch2){
Toast.makeText(getApplicationContext(), "No Changes " ,Toast.LENGTH_LONG).show();
}
else
Search();
MainActivity
public class MainActivity extends Activity {
private Thread thread;
Button btnShowLocation;
double latitude,longitude,Ch1,Ch2;
final Handler handler = new Handler();
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Search();
Ch1=gps.getLatitude();
Ch2=gps.getLongitude();
update();
}
private void update() {
(new Thread(new Runnable()
{
@Override
public void run()
{
while (!Thread.interrupted())
try
{
Thread.sleep(20000);
runOnUiThread(new Runnable() // start actions in UI thread
{
double a=gps.getLatitude();
double b=gps.getLongitude();
@Override
public void run()
{
if ( a==Ch1 && b==Ch2){
Toast.makeText(getApplicationContext(), "No Changes " ,Toast.LENGTH_LONG).show();
}
else
Search(); // this action have to be in UI thread
}
});
}
catch (InterruptedException e)
{
// ooops
}
}
})).start(); // the while thread will start in BG thread
}
private void Search() {
// TODO Auto-generated method stub
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
String sms="http://maps.google.com/maps?q=" + latitude + "," + longitude + "&iwloc=A";
String number = "012345";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, sms, null, null);
Toast.makeText(getApplicationContext(), "Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}
else{
gps.showSettingsAlert();
}
}
}
答案 0 :(得分:1)
Location locationA = new Location("point A");
locationA.setLatitude(a);
locationA.setLongitude(b);
Location locationB = new Location("point B");
locationB.setLatitude(Ch1);
locationB.setLongitude(Ch2);
float distance = locationA.distanceTo(locationB);