android判断GPS是否开启

判断手机GPS是否开启
下边这小段代码是用来判断手机的GPS服务是否为开启状态.如果是就提示用户GPS已经打开.

如果现在GPS 处于关闭状态,那么给用户一个提示, 然后打开GPS设置界面,让用户更改GPS为启动状态.

JAVA代码:

private void openGPSSettings()
{
LocationManager alm =
(LocationManager)this.getSystemService( Context.LOCATION_SERVICE );
if( alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
{
Toast.makeText( this, “GPS is already on”, Toast.LENGTH_SHORT ).show();
}
else
{
Toast.makeText( this, “Please turn on GPS”, Toast.LENGTH_SHORT ).show();
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
startActivity(myIntent);
}
}

参考链接:http://blog.csdn.net/jkguang/archive/2011/04/07/6307523.aspx


--------------------------------------------------------


private void initGPS(){
      LocationManager locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

      //判断GPS模块是否开启,如果没有则开启
      if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){
       Toast.makeText(this, "GPS is not open,Please open it!", Toast.LENGTH_SHORT).show();
       Intent intent=new Intent(Settings.ACTION_SECURITY_SETTINGS);
       startActivityForResult(intent,0);
      }
      else {
       Toast.makeText(this, "GPS is ready", Toast.LENGTH_SHORT);
      }
     }

参考链接:http://hi.baidu.com/yimifeitian/blog/item/ed6a92d7fe19783906088b04.html

你可能感兴趣的:(android)