我们可以递增或动态更改 android 中标志变量的名称吗?
Can we increment or dynamically change the name of flag variable in android?
我正在开发一个通过短信发送 Proximity alerts
的应用程序。其中它向 SQLite Database
中的号码发送短信。纬度和经度值也存储在数据库中。现在,只要 phone 输入适当的 LatLng
,我的代码就会发送 SMS 到用 LatLng
保存的号码。现在我想更改代码以在中午 12 点之前仅向所有号码发送一次短信。当他们在中午 12 点之后进入附近时,我想向所有号码发送一次短信。即每个数字不超过 12 点之前的一条短信和 12 点之后的一条短信。
我使用以下代码通过初始化 Flag Variable
来尝试。发送 SMS 后,标志值会更改并防止再次发送 SMS。但我遇到了一个问题,它向最接近的号码发送一次短信。无法发送所有下一个接近度。我的密码是
Code for Adding Proximity Alerts
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 is
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);
}
}
我是 Place SMS 的开发者 - 一款根据位置自动发送短信的应用。实际上,您在第一次循环迭代时将 flag 设置为 false。之后不执行其余迭代。将下面的代码移到循环外:
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",false);
editor.commit();
editor.clear();
我正在开发一个通过短信发送 Proximity alerts
的应用程序。其中它向 SQLite Database
中的号码发送短信。纬度和经度值也存储在数据库中。现在,只要 phone 输入适当的 LatLng
,我的代码就会发送 SMS 到用 LatLng
保存的号码。现在我想更改代码以在中午 12 点之前仅向所有号码发送一次短信。当他们在中午 12 点之后进入附近时,我想向所有号码发送一次短信。即每个数字不超过 12 点之前的一条短信和 12 点之后的一条短信。
我使用以下代码通过初始化 Flag Variable
来尝试。发送 SMS 后,标志值会更改并防止再次发送 SMS。但我遇到了一个问题,它向最接近的号码发送一次短信。无法发送所有下一个接近度。我的密码是
Code for Adding Proximity Alerts
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 is
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);
}
}
我是 Place SMS 的开发者 - 一款根据位置自动发送短信的应用。实际上,您在第一次循环迭代时将 flag 设置为 false。之后不执行其余迭代。将下面的代码移到循环外:
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("flag",false);
editor.commit();
editor.clear();