2017-09-12 64 views
2

我是Android Studio新手。作爲短信發送當前位置Android Studio

我試圖發送當前位置作爲單擊按鈕時的文本消息,但沒有任何反應,當我點擊。

首先,這裏是我的XML:

<Button android:id="@+id/emergency" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="14dp" 
     android:layout_marginStart="14dp" 
     android:background="@drawable/button_selector1" 
     android:drawableEnd="@android:drawable/ic_dialog_alert" 
     android:gravity="center" 
     android:maxLines="1" 
     android:paddingLeft="15dip" 
     android:paddingRight="15dip" /> 

然後,這裏是我的MainActivity代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.ly_home); 

    emergency = (Button) findViewById(R.id.emergency); 

    emergency.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      GPStracker g = new GPStracker(getApplicationContext()); 
      Location l = g.getLocation(); 
      if (l != null){ 
       double lat = l.getLatitude(); 
       double lon = l.getLongitude(); 
       String message="http://maps.google.com/maps?saddr="+lat+","+lon; 
       String number = "xxxxxxxx"; 
       SmsManager smsManager = SmsManager.getDefault(); 
       StringBuffer smsBody = new StringBuffer(); 
       smsBody.append(Uri.parse(message)); 
       android.telephony.SmsManager.getDefault().sendTextMessage(number, null, smsBody.toString(), null,null); 
      } 
     } 
    }); 
} 

而且,我的GPStracker類:​​

public class GPStracker implements LocationListener{ 

    Context context; 
    public GPStracker(Context c){ 
     context = c; 
    } 

    public Location getLocation(){ 

     LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if (isGPSEnabled){ 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,6000,10,this); 
      Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      return l; 
     }else{ 
      Toast.makeText(context,"Please enable GPS", Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

我給的權限在清單中:精細的位置,粗略的位置和收發短信。我究竟做錯了什麼?

+0

你有沒有得到任何位置? (在通過短信發送之前) – user3808887

回答

0

按照下面的答案 https://stackoverflow.com/a/43199211/6835152

有兩種不同的方法來檢索位置,一個是基於網絡,另一個是基於GPS服務。當然,基於GPS服務的位置總是更加準確。根據該文件,可能需要一些時間才能使用GPS服務接收位置。在這種情況下,通過使用getLastKnownLocation()方法可以使用最後一個已知位置來獲取最近可用的位置。在這裏,要獲取最後一個可用位置,您應該將LocationManager.GPS_PROVIDER更改爲LocationManager.NETWORK_PROVIDER。

如果您希望接收基於GPS服務的準確實時位置信息,我建議您使用FusedLocationApi。你可以在這裏找到https://developer.android.com/training/location/receive-location-updates.html

+0

在您的代碼中,您沒有收到上次已知的位置 –

+0

因此,我應該更改LocationManager.NETWORK_PROVIDER的LocationManager.GPS_PROVIDER,並且這樣我會收到最後一個已知位置? – user8559076

+0

是的,你會收到位置,但我更喜歡使用融合的位置API,因爲您必須添加谷歌播放服務,這也是很容易集成的。 –

0

使用位置服務和融合位置API 您是否添加了對Google Play服務的依賴關係?

以下是獲取當前位置的tutorial

您也可以找到代碼發送短信here