如何避免 GPS 状态问题 - 在我的 Android 应用程序中暂时不可用
我正在编写一个Android应用程序来获取我的设备的位置,它总是显示GPS状态为暂时不可用,即使我正在更改我的手机的位置,它甚至没有调用位置更改方法,您能调查一下这个问题吗?下面的代码
ls= (ArrayList<String>)lm.getProviders(false);
Log.e(""," SIZE "+ls.size());
sb=new StringBuilder();
for(int i=0;i<ls.size();i++)
{Log.e("","Data : "+ls.get(i));
lp=lm.getProvider(ls.get(i));
sb.append("\nProvider Name : "+lp.getName());
sb.append("\nPower Requirement :"+lp.getPowerRequirement());
sb.append("\nAccuracy : "+lp.getAccuracy()+"\n\n");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
Location location=lm.getLastKnownLocation(ls.get(i));
}
strt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv=(TextView)findViewById(R.id.TextView01);
tv.setText("StarteD :"+sb);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv=(TextView)findViewById(R.id.TextView01);
tv.setText("STOPPED ys :"+res);
Location location=lm.getLastKnownLocation(ls.get(1));
if(location==null)
Log.e("","location object null");
try{
tv.append("\nLogitude: "+location.getLongitude());
tv.append("\nLattitude: "+location.getLatitude());
tv.append("\nTime:"+location.getTime());
tv.append("\nSpeed: "+location.getSpeed());
tv.append("\n Provider:"+location.getProvider());
}catch(Exception e){}
}
});
}
@Override
public void onLocationChanged(Location location) {
Log.e("","LocationChandgeD... ");
}
@Override
public void onProviderDisabled(String provider) {
Log.e("",provider+" DIsabled... ");
}
@Override
public void onProviderEnabled(String provider) {
Log.e("",provider+" Enabled... ");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.e("", "Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
I am writing an android application to get location of my device, It is always showing GPS status as TEMPORARILY UN AVAILABLE, its even not calling Location Changed method even though i am changing location of my mobile, could you please look into the issue with the below code
ls= (ArrayList<String>)lm.getProviders(false);
Log.e(""," SIZE "+ls.size());
sb=new StringBuilder();
for(int i=0;i<ls.size();i++)
{Log.e("","Data : "+ls.get(i));
lp=lm.getProvider(ls.get(i));
sb.append("\nProvider Name : "+lp.getName());
sb.append("\nPower Requirement :"+lp.getPowerRequirement());
sb.append("\nAccuracy : "+lp.getAccuracy()+"\n\n");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
Location location=lm.getLastKnownLocation(ls.get(i));
}
strt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv=(TextView)findViewById(R.id.TextView01);
tv.setText("StarteD :"+sb);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv=(TextView)findViewById(R.id.TextView01);
tv.setText("STOPPED ys :"+res);
Location location=lm.getLastKnownLocation(ls.get(1));
if(location==null)
Log.e("","location object null");
try{
tv.append("\nLogitude: "+location.getLongitude());
tv.append("\nLattitude: "+location.getLatitude());
tv.append("\nTime:"+location.getTime());
tv.append("\nSpeed: "+location.getSpeed());
tv.append("\n Provider:"+location.getProvider());
}catch(Exception e){}
}
});
}
@Override
public void onLocationChanged(Location location) {
Log.e("","LocationChandgeD... ");
}
@Override
public void onProviderDisabled(String provider) {
Log.e("",provider+" DIsabled... ");
}
@Override
public void onProviderEnabled(String provider) {
Log.e("",provider+" Enabled... ");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.e("", "Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,如果没有清晰的天空视野(室内或茂密的针叶树覆盖范围),GPS 就无法工作。也许你应该尝试到户外去测试你的程序。
Unfortunaitly GPS doesn't work without a clear view of the sky (indoors or around dense coniferous tree coverage). Maybe you should try moving outdoors to test your program.