2015-08-08 21 views
0

我正在开发一个应用程序,通过短信发送Proximity alerts。其中它将短信​​发送到SQLite Database中的号码。纬度和经度值也存储在数据库中。现在,只要手机输入适当的LatLng,我的代码就会发送短信至LatLng保存的号码。现在我想更改代码,在中午12点之前只发送一次短信给所有号码。我想在中午12点后进入附近时向所有号码发送一条短信。即12之前的一个短信和12之后的一个短信,每个数字不超过那个。 我使用下面的代码尝试初始化Flag Variable。发送短信后,标志值发生变化,并阻止再次发送短信。但是我遇到了一个问题,它首先发送短信给第一次接近的号码。无法发送所有下一个附件。我的代码是我们可以在android中增加或动态更改标志变量的名称吗?

代码添加接近警报

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(); 
     } 

接近警报广播接收器是

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

我的开发板的每个地方的短信 - 一个应用程序,自动发送由位置触发的短信。实际上,您在第一次循环迭代时将标志设置为false。之后,其余的迭代不会被执行。将以下代码移到循环外部:

SharedPreferences.Editor editor=preferences.edit(); 
          editor.putBoolean("flag",false); 
          editor.commit(); 
          editor.clear(); 
相关问题