我想在启动应用程序时找出GPS状态。为此,我之前使用过LocationListener
类,
仅在设备中更改GPS状态时,它显示GPS的状态。
但这是我的要求:当我首先启动应用程序时,它应该检查GPS状态,如果GPS可用,那么我只想启动我的活动,否则它需要向用户显示一些消息,如“请启用GPS“。
我该怎么做?
答案 0 :(得分:1)
我认为这种方法可以帮到你:
public static boolean isGPSAvailable(Context ctx){
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
return lm != null &&
(lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
}//isGPSAvailable
答案 1 :(得分:1)
您可以检查您的提供商是否为空,并要求用户转到系统设置以启用GPS
这是代码
locationManager = (LocationManager) getSystemService(context);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(criteria, true);
if(provider==null){
new AlertDialog.Builder(this)
.setTitle("Location Settings")
.setMessage("Application needs your location,do you want to go to\n" +
"system settings to enable your location source?")
.setIcon(android.R.drawable.ic_menu_mapmode)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}catch(Exception e){
e.printStackTrace();
}
}})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LocationBasedServicesV1.this.finish();
}
})
.show();
}