我有一项任务,应用程序使用LocationManager
和GPS_PROVIDER
来监听位置Latitude
和Longitude
并将其存储在临时内存中一段时间。当临时存储器达到六LatLngs
时,它会通过SMS
将其内容发送到手机号码,内容必须变为null
。
我使用了以下代码
private void sendLog() {
Toast.makeText(MainPage.this,"Sending Log",Toast.LENGTH_LONG).show();
final SharedPreferences account=getSharedPreferences("admins",MODE_PRIVATE);
String interval=account.getString("lti", "");
int timeInterval=Integer.parseInt(interval);
LocationManager logManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
logManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
DecimalFormat dFormat = new DecimalFormat("#.####");
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
String time=sdf.format(date);
List<String> loglist = new ArrayList<String>();
for (int i=1;i<=6;i++){
loglist.add("!+" + dFormat.format(latitude) + ",+" + dFormat.format(longitude) + "," + time);
}
Toast.makeText(MainPage.this,loglist.toString(),Toast.LENGTH_LONG).show();
if (loglist.size()==6){
StringBuilder log = new StringBuilder();
for (int j=0;j<loglist.size();j++){
log.append(loglist.get(j).toString());
}
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(logPreferences.getString("admin1",""),null,log.toString(),null,null);
}
}
此代码获取LatLng并发送带有六个值的sms而不会失败。但问题是它在阵列列表的所有位置只添加一个纬度和经度值。我不想在各个方面获得相同的价值。我想在ArrayList中有六个不同的纬度和经度值。如果有人知道怎么做,请帮助我。谢谢。
答案 0 :(得分:0)
List<String> loglist = new ArrayList<String>();
for (int i=1;i<=6;i++){
if(!loglist.contains(dFormat.format(latitude)+","+dFormat.format(longitude)+","+time)) //if not already added then add it
loglist.add(dFormat.format(latitude)+","+dFormat.format(longitude)+","+time);
}
检查是否尚未添加该值,如果没有,则添加
答案 1 :(得分:0)
因为您循环了相同的位置6次,请尝试以下代码:
List<String> loglist = new ArrayList<String>(); // declare your list outside the listener, or make it as class variable.
LocationManager logManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
logManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
DecimalFormat dFormat = new DecimalFormat("#.####");
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
String time=sdf.format(date);
Toast.makeText(MainPage.this,loglist.toString(),Toast.LENGTH_LONG).show();
if (loglist.size()==6){// you reach 6 values => send the SMS
StringBuilder log = new StringBuilder();
for (int j=0;j<loglist.size();j++){
log.append(loglist.get(j).toString());
}
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(logPreferences.getString("admin1",""),null,log.toString(),null,null);
} else {// you can add more location
loglist.add("!+" + dFormat.format(latitude) + ",+" + dFormat.format(longitude) + "," + time);
}
}