Android 让gps不工作
有人能解释一下为什么我的代码不起作用吗? 我试着尽可能容易地获得全球定位系统。调试器没有发现任何错误,但当我运行它时,它就是不工作。我可以启用GPS和禁用GPS的msges,但不能启用GPS纬度和经度 下面是代码:Android 让gps不工作,android,gps,Android,Gps,有人能解释一下为什么我的代码不起作用吗? 我试着尽可能容易地获得全球定位系统。调试器没有发现任何错误,但当我运行它时,它就是不工作。我可以启用GPS和禁用GPS的msges,但不能启用GPS纬度和经度 下面是代码: public class getgps extends AppCompatActivity { double longtitude; double latitude; @Override protected void onCreate(Bundle savedInstanceS
public class getgps extends AppCompatActivity {
double longtitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getgps);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
latitude = loc.getLatitude();
longtitude = loc.getLongitude();
String Text = "My current location is: " +
"Latitude = " + latitude +
"Longitude = " + longtitude;
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(), + latitude +" ",
Toast.LENGTH_SHORT).show();
}
}
@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_getgps, 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);
}
}
谢谢你的建议^_- 正如我在上面的评论中提到的,请确保您至少在open sky下测试应用程序一次。只有这样,GPS才能得到固定,你才能得到GPS数据 正如我在上面的评论中提到的,请确保您至少在open sky下测试应用程序一次。只有这样,GPS才能得到固定,你才能得到GPS数据 尝试更改
mlocManager.requestLocationUpdates(LocationManager.GPS\u提供程序,5000,10,mlocListener)代码>到mlocManager.requestLocationUpdates(LocationManager.GPS\u提供程序,0,0,mlocListener)代码>。还要确保你应该第一次在open sky中测试该应用程序。我也尝试了0,0,但它不起作用。现在我在开阔的天空中试过了,就像你告诉我的^^^^而且成功了。但这很有趣,因为谷歌地图或waze即使在豪斯也没有问题。谢谢,太好了。我会把它作为我的答案添加。如果答案真的对您有帮助,请您将其标记为已接受。尝试更改mlocManager.requestLocationUpdates(LocationManager.GPS\u PROVIDER,5000,10,mlocListener)代码>到mlocManager.requestLocationUpdates(LocationManager.GPS\u提供程序,0,0,mlocListener)代码>。还要确保你应该第一次在open sky中测试该应用程序。我也尝试了0,0,但它不起作用。现在我在开阔的天空中试过了,就像你告诉我的^^^^而且成功了。但这很有趣,因为谷歌地图或waze即使在豪斯也没有问题。谢谢,太好了。我会加上这个作为我的答案。如果这个答案真的对你有帮助的话,你能把它标记为接受吗。。