Android 通过单个SMS发送设备GPS位置
我真的试图寻找代码和信息,事实上我的代码部分是基于这个网站的其他问题和答案。尽管如此,我不知道我做错了什么。 此代码位于后台服务中。我试着用这段代码来做的,基本上就是发送一条短信,以gpsTimeValue的周期为单位显示基于GPS的位置。我知道。睡眠(时间)不是测量时间的准确方法,但它并不重要Android 通过单个SMS发送设备GPS位置,android,gps,sms,location,locationmanager,Android,Gps,Sms,Location,Locationmanager,我真的试图寻找代码和信息,事实上我的代码部分是基于这个网站的其他问题和答案。尽管如此,我不知道我做错了什么。 此代码位于后台服务中。我试着用这段代码来做的,基本上就是发送一条短信,以gpsTimeValue的周期为单位显示基于GPS的位置。我知道。睡眠(时间)不是测量时间的准确方法,但它并不重要 if (locateMode) // start location manager locationManager = (LocationManager) getSystemService(Contex
if (locateMode)
// start location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// entering locate mode
while (locateMode) {
handler.post(new DisplayToast("Entering locate mode"));
// check value of sleep time
gpsTimeValue = Integer.parseInt(pref.getString("gps_time_value", getString(R.string.gpsTimeDefault))) * 6000;
// update notification
msg = "Starting GPS cycle with " + gpsTimeValue;
handler.post(new DisplayToast(msg));
notificationBuilder.setContentText(msg);
notificationManager.notify(notificationId,
notificationBuilder.build());
// send location sms
sendLocationSMS(pref.getString("send_to_number", ""),locationManager);
// sleep
try {
Thread.sleep(gpsTimeValue);
} catch (InterruptedException e) {
e.printStackTrace();
}
// check location mode
if (pref.getString("locate_mode", "OFF").equalsIgnoreCase("OFF")) {
locateMode = false;
handler.post(new DisplayToast("Exiting locate mode"));
}
}
这是sendLocationSMS的代码。我要明确基于GPS的位置:
private void sendLocationSMS(final String sendToNumber,
LocationManager locationManager) {
handler.post(new DisplayToast("Seting up SMS to number: "+ sendToNumber));
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the listener
String loc = location.toString();
String message = "Location" + loc;
handler.post(new DisplayToast(message));
if (!sendToNumber.isEmpty()) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(
sendToNumber, null,message,null, null);
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {}
};
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,0, locationListener);
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, null);
}
因此,除了onLocationChange中的代码之外,每个toast(我使用handler.post(newDisplay(message))都会显示出来。我在手机中看到位置是如何被激活的(我想是因为侦听器的设置),但没有消息被发送,也没有位置被生成。我尝试了RequestLocationUpdate和requestSingleUpdate
除其他外,我在清单中拥有与此代码相关的权限,如下所示:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
除此之外,我的应用程序中所有代码的其他部分都像一个符咒。我快发疯了,我不知道怎么解决它。请,任何建议都将非常受欢迎。谢谢。这将有助于您通过邮件获取位置详细信息 locationText=location.getLatitude()+,“+location.getLatitude() smsManager.sendTextMessage(phoneNo,null,locationText,null,null)