image.png
LocationManager是Android 提供的Location 服务,来获得当前的位置信息和卫星信息。
要使用它,先得获得系统所提供的location_service.
LocationManager lm;
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
定位服务的提供一般有3种:
通过api获取现有支持的定位服务模式:
lm.getAllProviders();
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/pro"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="20dp"
tools:layout_editor_absoluteY="20dp" />
</android.support.constraint.ConstraintLayout>
代码文件:
package com.example.user.location;
import android.content.Context;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView providers;
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
providers = (ListView)findViewById(R.id.pro);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
List<String> provideNames = lm.getAllProviders();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
provideNames);
providers.setAdapter(adapter);
}
}
使用requestLocationUpdates
注册监听,定时获取定位hal service返回的数据
整体项目如下:
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.gps">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
active_main.xml
package com.example.user.gps;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.user.gps", appContext.getPackageName());
}
}
代码:
package com.example.user.gps;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView positionTextView;
private TextView positionLatLng;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView=(TextView)findViewById(R.id.position_text_view);
positionLatLng=(TextView)findViewById(R.id.position_plain_text);
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
provider=LocationManager.GPS_PROVIDER;
Location location=locationManager.getLastKnownLocation(provider);
if(location!=null){
//显示当前设备的位置信息
Log.d("lidu---", "location!=null");
showLocation(location);
}
Log.d("lidu---", "location==null");
locationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
}
//LocationListener 用于当位置信息变化时由 locationManager 调用
LocationListener locationListener=new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("test", "onLocationChanged");
//更新当前设备的位置信息
showLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
private void showLocation(final Location location){
StringBuilder sb = new StringBuilder();
sb.append(location.getLatitude());
positionTextView.setText(sb.toString());
}
}