我正在开发一个通过短信发送Proximity alerts
的应用。其中,它向SQLite Database
中的号码发送短信。纬度和经度值也存储在数据库中。现在我的代码会在手机输入适当的LatLng
时发送,它会向使用LatLng
保存的号码发送短信。现在我想更改代码,在中午12点之前只向所有号码发送一次短信。我想在中午12点之后进入他们的邻近区域时向所有号码发送一条短信。即12个之前的一个sms和12个之后的一个sms,每个数字不超过该数量。
我使用以下代码通过初始化Flag Variable
来尝试。发送短信后,标志值会发生变化,并阻止再次发送短信。但我遇到了一个问题,它发送短信一次到最近的数字。无法发送所有下一个邻近区域。我的代码是
添加邻近警报的代码
try {
databaseHelper = new DatabaseHelper(this);
db = databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select rowid _id,*from latLngTable", null);
go = cursor.moveToFirst();
while (cursor.isAfterLast() != true) {
String strLat = cursor.getString(cursor.getColumnIndex("latitude"));
String strLng = cursor.getString(cursor.getColumnIndex("longitude"));
String strStopName = cursor.getString(cursor.getColumnIndex("Stop_Name"));
float strRadius = cursor.getFloat(cursor.getColumnIndex("radius"));
String mobileNo = cursor.getString(cursor.getColumnIndex("Phone_NO"));
double dLat = Double.parseDouble(strLat);
double dLng = Double.parseDouble(strLng);
if (strRadius==0.0){
radius=150;
}else {
radius=strRadius;
}
LocationManager locationManager2 = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager2.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
String proximitys = "com.realtech.latlngrecorder" + n;
Intent i = new Intent(proximitys);
i.putExtra("phone", mobileNo);
i.putExtra("stopName", strStopName);
sendBroadcast(i);
PendingIntent pi = PendingIntent.getBroadcast(this, n, i, PendingIntent.FLAG_CANCEL_CURRENT);
locationManager2.addProximityAlert(dLat, dLng, radius, -1, pi);
IntentFilter filter = new IntentFilter(proximitys);
registerReceiver(new ProximityIntentReceiver(), filter);
Log.w("Alert Added", "Alert Added to All Locations");
n++;
cursor.moveToNext();
}
} catch (SQLiteException e) {
e.printStackTrace();
Toast.makeText(MainPage.this, "Failed to add Proximity Alert", Toast.LENGTH_SHORT).show();
}
Proximity Alert BroadcastReceiver是
public class ProximityIntentReceiver extends BroadcastReceiver {
String smsNo;
String lat,lng,stopingName,lat1,lng1;
Double latitude,longitude,latitude1,longitude1;
DecimalFormat dFormat,dFormat1;
boolean flag=true;
SharedPreferences preferences;
@Override
public void onReceive(final Context context, Intent intent) {
smsNo=intent.getStringExtra("phone");
stopingName=intent.getStringExtra("stopName");
preferences=context.getSharedPreferences("flagTest",Context.MODE_PRIVATE);
if (!preferences.contains("flag")){
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",true);
editor.commit();
editor.clear();
}
final String proximityKey= LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering=intent.getBooleanExtra(proximityKey,false);
if (entering){
Log.d(getClass().getSimpleName(), "entering");
Calendar morningStart= Calendar.getInstance();
Calendar morningEnd = Calendar.getInstance();
setTime(morningStart,7);
setTime(morningEnd, 10);
Calendar eveningStart = Calendar.getInstance();
Calendar eveningEnd = Calendar.getInstance();
setTime(eveningStart, 15);
setTime(eveningEnd, 19);
Calendar currentTime = Calendar.getInstance();
Calendar limitTime = Calendar.getInstance();
setTime(limitTime,12);
boolean flagVar=preferences.getBoolean("flag",true);
if (currentTime.after(limitTime)&&flag==flagVar){
try {
final SmsManager smsManager = SmsManager.getDefault();
String[] numbers = smsNo.split(",");
for (final String number : numbers) {
final LocationManager locationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
dFormat=new DecimalFormat("#.####");
latitude=location.getLatitude();
longitude=location.getLongitude();
lat=dFormat.format(latitude);
lng=dFormat.format(longitude);
smsManager.sendTextMessage(number, null, "Stop:" + stopingName + "\n\nSchool Van will reach you shortly.\n\nhttp://maps.google.com/maps?q=" + lat + "," + lng, null, null);
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",false);
editor.commit();
editor.clear();
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
}catch (Exception e){
Toast.makeText(context,"No mobile Number Registered",Toast.LENGTH_SHORT).show();
}
}else if (currentTime.before(limitTime)&&flag==preferences.getBoolean("flag",false)){
try {
final SmsManager smsManager = SmsManager.getDefault();
String[] numbers = smsNo.split(",");
for (final String number : numbers) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
dFormat1 = new DecimalFormat("#.####");
latitude1 = location.getLatitude();
longitude1 = location.getLongitude();
lat1 = dFormat1.format(latitude1);
lng1 = dFormat1.format(longitude1);
smsManager.sendTextMessage(number, null, "Stop:" + stopingName + "\n\nSchool Van will reach you shortly.\n\nhttp://maps.google.com/maps?q=" + lat + "," + lng, null, null);
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",true);
editor.commit();
editor.clear();
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
}catch (Exception e){
Toast.makeText(context,"No Mobile number registered",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(context,"Time Flag Exception",Toast.LENGTH_SHORT).show();
}
}else {
Log.d(getClass().getSimpleName(), "exiting");
Toast.makeText(context,"exiting",Toast.LENGTH_SHORT).show();
}
}
private void setTime(Calendar calendar, int hour) {
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}
}
答案 0 :(得分:0)
我是Place SMS的开发者 - 一个自动发送由位置触发的短信的应用。实际上,您在第一次循环迭代时将标志设置为false。之后不执行其余迭代。将以下代码移到循环外部:
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",false);
editor.commit();
editor.clear();