自动发送带GPS位置的短信

时间:2015-06-16 09:14:54

标签: android gps smsmanager

我正在开发一个应用,在固定时间后通过SMS发送用户位置。     我几乎完成了我的任务。我只是在GPS位置未发送SMS时才会遇到问题,但是当我在GPS时,发送的短信无效。

String myAddress = "";

List<Address> location;
private void getAddress() {
    // TODO Auto-generated method stub



     try {

    startService(new Intent(mContext,GPSTracker.class));
    GPSTracker tracker = new GPSTracker(mContext);

    location = tracker.getGeocoderAddress(mContext);  

    if (tracker.canGetLocation() == false) 
       {
        tracker.showSettingsAlert();
       }
       else 
       {
           GlobalValues.location_home_lat = tracker.getLongitude();
           GlobalValues.location_home_lng = tracker.getLatitude();

       }


  if(location != null) {
   Address returnedAddress = location.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("location:\t");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
   }
   myAddress = strReturnedAddress.toString();



  }




private void countdownStart() {
    // TODO Auto-generated method stub
    detectionStart.setBase(SystemClock.elapsedRealtime());
    detectionStart.start();

    detectionStart.setOnChronometerTickListener(new OnChronometerTickListener() {

        @Override
        public void onChronometerTick(Chronometer chronometer) {
            // TODO Auto-generated method stub
            long elapsedMillis = SystemClock.elapsedRealtime() - detectionStart.getBase();
                Date date = new Date(elapsedMillis);
                DateFormat formatter = new SimpleDateFormat("s");
                String dateFormatted = formatter.format(date);
                detectionStart.setText(dateFormatted);

            if(elapsedMillis>THRESHOLD){
                if(userNumber.length()>0 && message.length()>0){
                sendSms(userNumber,message);
                }
                else{
                    Toast.makeText(getBaseContext(), 
                            "Error in sendind sms", 
                            Toast.LENGTH_SHORT).show();
                }
                stopAlarm();
                onCloseActivity();
                goMainActivity();
            }
        }
    });
}


protected void sendSms(String number, String message) {
    // TODO Auto-generated method stub  
     String SENT = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
     String DELIVERED = "SMS_DELIVERED";

     PendingIntent sentPI = PendingIntent.getBroadcast(DoorbellDetectedActivity.this, 0,
             new Intent(SENT), 0);

         PendingIntent deliveredPI = PendingIntent.getBroadcast(DoorbellDetectedActivity.this, 0,
             new Intent(DELIVERED), 0);


    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(userNumber, null, message, null, null);  
}

2 个答案:

答案 0 :(得分:0)

可能你做错了。倒数计时器从哪里开始?您应该只在获得位置时发送短信。这意味着您应该将代码移动到GPS跟踪类的onlocationchanged()回调中。请重新查看代码,您的SMS发送逻辑与位置回调无关,而是与计时器绑定。

答案 1 :(得分:0)

    final LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        final LocationListener mlocListener = new MyLocationListener();

        final Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateWithNewLocation(location);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        mc=mapview.getController();
        mc.setZoom(18);
        mlo=new MyLocationOverlay(this,mapview);
        mapview.getOverlays().add(mlo);
        mapview.invalidate();   
        }




        public class MyLocationListener implements LocationListener

        {

        public void onLocationChanged(final Location location)
        {
            updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        }

        private void updateWithNewLocation(final Location location)
        {
        mapview=(MapView) findViewById(R.id.map);
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.text);
        String addressString = "No address found";

        if ( location != null )
        {
        final double lat = location.getLatitude();
        final double lng = location.getLongitude();

        latLongString = "Lat:" + lat + "\nLong:" + lng;
        final double latitude = location.getLatitude();
        final double longitude = location.getLongitude();
        addressString = String.format("%f, %f", latitude, longitude);
        }
        catch ( final IOException e )
        {
            System.err.println(e.toString());
        }
        }
        else
        {
        latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is:\n" +
 addressString);


        }





        @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
            MenuInflater inf = getMenuInflater();
            inf.inflate(R.menu.newmenu, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            String tv1; 
            TextView myLocationText;
            myLocationText = (TextView)findViewById(R.id.text);
            tv1= myLocationText.getText().toString(); 
            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", tv1); 
            sendIntent.setType("vnd.android-dir/mms-sms");
            startActivity(sendIntent); 
            return(super.onOptionsItemSelected(item));
       }

希望有所帮助

相关问题