2015-03-31 88 views
1

我是android技術的新手。就在我開始學習之前的一個星期。我試圖用我的註冊手機號碼發送我的位置的詳細信息。它只在SONY手機上運行良好。誰能幫我。這是我正在使用的代碼。如何發送有關我的位置地址的短信android

private void SendSMsandGeocoder() { 



    Geocoder ageocoder=new Geocoder(myContext); 
    try { 
     TelephonyManager atele=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     String adata=atele.getDeviceId(); 

     AccountManager AAcm=AccountManager.get(myContext); 
     Account[] aAccarray=AAcm.getAccounts(); 
     Log.e("account manager",aAccarray.toString()); 
     for (Account ac : aAccarray) { 
      String acname = ac.name; 
      String actype = ac.type; 
      Log.e("acname",acname); 
      Log.e("actype", actype); 
      Log.e("account details",acname+actype); 
      myAcnameget= acname; 
     } 


     double mySourcelat =mygpsclass.Findlatitude(); 
     double mySourcelng =mygpsclass.FindLongitude(); 
     Log.e("Gps cordinates", mySourcelat+","+mySourcelng); 
     List<Address> aList=ageocoder.getFromLocation(mySourcelat, mySourcelng, 2); 
     String aPlacename=aList.get(0).getLocality(); 
     Log.e("location name",aPlacename); 
     String aSublocality=aList.get(0).getSubLocality(); 
     Log.e("Sub locality",aSublocality); 
     String mySourceplace=mySourcelat+","+mySourcelng; 
     String aaddress=aList.get(0).getFeatureName(); 
     Log.e("feature name",aaddress); 

     String aaddress1=aList.get(0).getPostalCode(); 
     Log.e("through fare",aaddress1); 
     String aAddressfull=aSublocality+","+aaddress+","+aPlacename+","+aaddress1; 
     String aSendmsgcontent="The User Of the IMEI no:"+adata+" was in Co-ordinates:" +mySourceplace+" and their address was "+aAddressfull+","+ myAcnameget; 
     Log.e("Full address With msg", aSendmsgcontent); 
     myHashmap=myDbclass.GetConDetails(); 
     myCon1=myHashmap.get("Contact1"); 
     Log.e("Contact 1",myCon1); 
     myCon2=myHashmap.get("Contact2"); 
     Log.e("Contact 2",myCon2); 
     myCon3=myHashmap.get("Contact3"); 
     Log.e("Contact 3",myCon3); 
     myCon4=myHashmap.get("Contact4"); 
     Log.e("Contact 4",myCon4); 
     myCon5=myHashmap.get("Contact5"); 
     Log.e("Contact 5",myCon5); 
     SmsManager aSms=SmsManager.getDefault(); 
     aSms.sendTextMessage(myCon1, null,aSendmsgcontent, null, null); 
     aSms.sendTextMessage(myCon2, null,aSendmsgcontent, null, null); 
     aSms.sendTextMessage(myCon3, null,aSendmsgcontent, null, null); 
     aSms.sendTextMessage(myCon4, null,aSendmsgcontent, null, null); 
     aSms.sendTextMessage(myCon5, null,aSendmsgcontent, null, null); 
    } catch (Exception e) { 
     Log.e("sms", e.toString()); 
    } 
} 

回答

1

@ARAVIND RAJ這很容易

在你的manifest.xml添加

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 

添加這在你的Java

GPSTracker gpsTracker = new GPSTracker(this); 
String temp; 

     if (gpsTracker.canGetLocation()) 
     { 
      String stringLatitude = String.valueOf(gpsTracker.latitude); 

      String stringLongitude = String.valueOf(gpsTracker.longitude); 

      String country = gpsTracker.getCountryName(this); 

      String city = gpsTracker.getLocality(this); 

      String postalCode = gpsTracker.getPostalCode(this); 

      String addressLine = gpsTracker.getAddressLine(this); 

      temp = stringLatitude + " " + stringLongitude ; 
     } 
     else 
     { 
      // can't get location 
      // GPS or Network is not enabled 
      // Ask user to enable GPS/network in settings 

     } 
    } 


String phoneNo = "123243444" ;// put yout phone number here 

    try {  
    SmsManager smsManager = SmsManager.getDefault(); 
    smsManager.sendTextMessage(phoneNo, null, temp, null, null);  
    Toast.makeText(getApplicationContext(), "Message Sent", 
     Toast.LENGTH_LONG).show(); 
    } catch (Exception ex) { 

    ex.printStackTrace(); 
    } 

使一個類文件,如下所示

public class GPSTracker extends Service implements LocationListener 
{ 
    private final Context mContext; 

    //flag for GPS Status 
    boolean isGPSEnabled = false; 

    //flag for network status 
    boolean isNetworkEnabled = false; 

    boolean canGetLocation = false; 

    Location location; 
    double latitude; 
    double longitude; 

    //The minimum distance to change updates in metters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters 

    //The minimum time beetwen updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    //Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) 
    { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() 
    { 
     try 
     { 
      locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 

      //getting GPS status 
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

      //getting network status 
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) 
      { 
       // no network provider is enabled 
      } 
      else 
      { 
       this.canGetLocation = true; 

       //First get location from Network Provider 
       if (isNetworkEnabled) 
       { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        Log.d("Network", "Network"); 

        if (locationManager != null) 
        { 
         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         updateGPSCoordinates(); 
        } 
       } 

       //if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) 
       { 
        if (location == null) 
        { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

         Log.d("GPS Enabled", "GPS Enabled"); 

         if (locationManager != null) 
         { 
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          updateGPSCoordinates(); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      //e.printStackTrace(); 
      Log.e("Error : Location", "Impossible to connect to LocationManager", e); 
     } 

     return location; 
    } 

    public void updateGPSCoordinates() 
    { 
     if (location != null) 
     { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
     } 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    */ 

    public void stopUsingGPS() 
    { 
     if (locationManager != null) 
     { 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

    /** 
    * Function to get latitude 
    */ 
    public double getLatitude() 
    { 
     if (location != null) 
     { 
      latitude = location.getLatitude(); 
     } 

     return latitude; 
    } 

    /** 
    * Function to get longitude 
    */ 
    public double getLongitude() 
    { 
     if (location != null) 
     { 
      longitude = location.getLongitude(); 
     } 

     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    */ 
    public boolean canGetLocation() 
    { 
     return this.canGetLocation; 
    } 


    /** 
    * Get list of address by latitude and longitude 
    * @return null or List<Address> 
    */ 
    public List<Address> getGeocoderAddress(Context context) 
    { 
     if (location != null) 
     { 
      Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); 
      try 
      { 
       List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); 
       return addresses; 
      } 
      catch (IOException e) 
      { 
       //e.printStackTrace(); 
       Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Try to get AddressLine 
    * @return null or addressLine 
    */ 
    public String getAddressLine(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String addressLine = address.getAddressLine(0); 

      return addressLine; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get Locality 
    * @return null or locality 
    */ 
    public String getLocality(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String locality = address.getLocality(); 

      return locality; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get Postal Code 
    * @return null or postalCode 
    */ 
    public String getPostalCode(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String postalCode = address.getPostalCode(); 

      return postalCode; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get CountryName 
    * @return null or postalCode 
    */ 
    public String getCountryName(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String countryName = address.getCountryName(); 

      return countryName; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 
} 
+0

如果這沒有幫助請告訴我.. – Ajeet 2015-03-31 08:14:41

+0

謝謝先生,它會工作。另一個更多的問題,而發送acount經理的細節和gps位置,它會顯示錯誤日誌:sms java.lang.nullpointerexception println需要消息。如何解決這個問題 – 2015-03-31 09:01:20

+0

你需要把你的「消息(位置)字符串」放在smsManager.sendTextMessage(phoneNo,null,msg,null,null);味精變量。嘗試使用String msg = temp;裏面try {}。你可以標記這個答案爲勾號,它會幫助我:) – Ajeet 2015-03-31 09:04:09

0

最後我發現了錯誤。我註釋了這兩行:

// String aaddress1=aList.get(0).getPostalCode(); 
// Log.e("through fare",aaddress1); 

我的代碼現在正常工作。非常感謝你@Ajeet。