2016-11-24 80 views
-1

我想通過短信發送用戶位置的用戶位置。我可以得到用戶的位置,但它需要大量的時間,而smsmanager發送帶有位置的空值有人可以看到我的代碼或引導我這裏有什麼問題。謝謝!我想通過短信發送android系統

public class MainActivity extends AppCompatActivity { 
    public Button button; 

    private LocationManager locationManager; 
    private LocationListener listener; 
    private String gpslonla; 
    private TextView t; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View view) { 
       getlocation(); 
       sendlocationsms(); 

      } 


      private void sendlocationsms() { 
       String phoneNumber = "903399000"; 
       //Location location = new Location("dummyprovider"); 

       SmsManager smsManager = SmsManager.getDefault(); 
       StringBuffer smsBody = new StringBuffer(); 
       smsBody.append("http://maps.google.com?q="+gpslonla); 

       //smsBody.append(location.getLatitude()); 
       //smsBody.append(","); 
       //smsBody.append(location.getLongitude()); 
       smsManager.sendTextMessage(phoneNumber, null, smsBody.toString(), null, null); 
       int gravity = Gravity.CENTER; // the position of toast 
       int xOffset = 0; // horizontal offset from current gravity 
       int yOffset = 0; // vertical offset from current gravity 
       Toast toast = Toast.makeText(getApplicationContext(), "Your message has been sent ", Toast.LENGTH_SHORT); 
       toast.setMargin(50, 50); 
       toast.setGravity(gravity, xOffset, yOffset); 
       toast.show(); 

      } 

     }); 

    } 

    private void getlocation() { 

     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       gpslonla=location.getLongitude() + "," + location.getLatitude(); 


      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 

      } 

      @Override 
      public void onProviderEnabled(String s) { 

      } 

      @Override 
      public void onProviderDisabled(String s) { 
       Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(i); 

      } 
     }; 
     configure_button(); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode) { 
      case 10: 
       configure_button(); 
       break; 
      default: 
       break; 
     } 
    } 

    void configure_button() { 
     // first check for permissions 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET} 
         , 10); 
      } 
      return; 
     } 
     // this code won't execute IF permissions are not allowed, because in the line above there is return statement. 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //noinspection MissingPermission 
       locationManager.requestLocationUpdates("gps", 5000, 0, listener); 
      } 
     }); 
    } 
} 
+0

你火的getLocation(); sendlocationsms();與此同時。當你發送短信時,你希望如何發送位置信息? – Stanojkovic

回答

1

當收到位置時,您必須調用sendlocationsms()方法。

編輯::與更改的onClick方法

聲明與初始值的布爾變量設置爲false:

boolean needsToSendSms = false; 

然後:

@Override 
    public void onClick(View view) { 
     needsToSendSms = true; 
     getlocation(); 
    } 

,並調用它當與位置的變化

@Override 
    public void onLocationChanged(Location location) { 
     gpslonla=location.getLongitude() + "," + location.getLatitude(); 
     if(needsToSendSms) { 
      sendlocationsms(); 
      needsToSendSms = false; 
     } 

    } 

添加布爾變量使得只發送每點擊一個短信。

我希望這會有所幫助。

+0

這個作品謝謝!但它一直在發送消息。像無限!我只想發送一條消息 – Yash

+0

創建一個名爲needsToSendSms的布爾變量,其初始值爲false,然後更改像我編輯的答案那樣的方法 – kelmi92

+0

我希望我編輯的答案適合您。如果是這樣,請再次將答案標記爲正確。 – kelmi92