有没有办法在没有任何许可的情况下检查Android GPS是打开还是关闭,只是我不需要切换它的状态。
由于
答案 0 :(得分:30)
没有。在Android上检查GPS状态需要
android.permission.ACCESS_FINE_LOCATION
检查GPS状态的代码
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
答案 1 :(得分:5)
步骤 -
您在Manifest文件中也需要以下权限 -
android.permission.ACCESS_FINE_LOCATION
编写代码
final LocationManager manager = (LocationManager)context.getSystemService (Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
//what you want to do
else
//what you want to do
或者只是你可以检查
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
连续运行您的服务以监控连接
答案 2 :(得分:2)
如果您想检查GPS的状态是开启还是关闭,那么此解决方案将帮助您
final LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
Toast.makeText(context, "GPS is disable!", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is Enable!", Toast.LENGTH_LONG).show();