[问题] GPS定位模拟器可以运作手机不行??

楼主: jerryxin (jerry)   2019-10-09 06:54:44
我想问大家
我写的Android 读取当前GPS的程式
写好后,在Android模拟器上能运作
但是在Android手机上读不出值(手机定位有打开,Android9.0 华硕手机)
public class MainActivity extends AppCompatActivity implements
LocationListener {
public static final String TAG = "MainActivity";
private TextView tvLocation;
private TextView tvUpdateCounts;
private LocationManager locationManager;
private int count;
private static final int REQUEST_LOCATION = 10;
Double longitude;
Double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocation = (TextView) findViewById(R.id.tvLocation);
tvUpdateCounts = (TextView) findViewById(R.id.tvUpdateCounts);
if(Build.VERSION.SDK_INT> Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
return;
}
}
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// Location location1 =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// tvLocation.setText("经度:"+location1.getLongitude()+"纬度:
"+location1.getLatitude());
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 0, this);
//tvLocation.setText("经纬度
"+.getLongitude()+","+location.getLongitude());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG,"onLocationChanged");
count++;
longitude=location.getLongitude();
latitude=location.getLatitude();
tvLocation.setText("经纬度"+longitude+","+latitude);
tvUpdateCounts.setText("更新次数:"+count);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Log.i(TAG,"onStatusChanged"+ tvLocation.toString());
}
@Override
public void onProviderEnabled(String s) {
Log.i(TAG,"onProviderEnabled");
}
@Override
public void onProviderDisabled(String s) {
Log.i(TAG,"onLocationChanged");
}
}
Layout:
<RelativeLayout 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">
<TextView
android:id="@+id/tvLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="经纬度"
android:textSize="24sp" />
<TextView
android:id="@+id/tvUpdateCounts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvLocation"
android:text="更新次数"
android:textSize="24sp" />
</RelativeLayout>
在模拟器上GPS位置有显示出来
但在手机上都没出来
这不知道是什么原因???
烦请高手解答
作者: kobe8112 (小B)   2019-10-09 09:33:00
Logcat说啥?

Links booklink

Contact Us: admin [ a t ] ucptt.com