1.第一种方法

private void toggleGPS() {

Intent gpsIntent = new Intent();

gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");

gpsIntent.addCategory("android.intent.category.ALTERNATIVE");

gpsIntent.setData(Uri.parse("custom:3"));

try {

PendingIntent.getBroadcast(StartActivity.this, 0, gpsIntent, 0).send();

} catch (CanceledException e) {

e.printStackTrace();

}

}

2.第二种方法

private void openGPSSettings() {

//获取GPS现在的状态(打开或是关闭状态)

boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER );

if(gpsEnabled)

{

//关闭GPS

Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, false );

}

else

{

//打开GPS www.55zm.com

Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, true);

}

3.第三种方法(手动设置)

LocationManager alm = (LocationManager)StartActivity.this.getSystemService(Context.LOCATION_SERVICE);

if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))

{

Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();

}

Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

startActivityForResult(intent,0); //此为设置完成后返回到获取界面

第一第二种需要加上权限

<!--允许程序读取或写入系统设置 -->

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

更多相关文章

  1. Android Phone模块 三
  2. Android沉浸式状态栏三种方式的实现
  3. Android利用反射获取状态栏(StatusBar)高度
  4. Android电池状态监听实例1(下)
  5. Android 沉浸式状态栏跟虚拟按键冲突问题 隐藏虚拟按键
  6. 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获
  7. Android-透明状态栏
  8. Android进入suspend状态(goToSleep)
  9. android状态机statemachine详解

随机推荐

  1. androidのDOM方式解析XML
  2. android与javascript交互调用
  3. Android(安卓)性能优化(一)
  4. Android中LCD背光驱动
  5. Android(安卓)应用程序快速启动的秘诀
  6. 【入门篇】Android学习笔记――常用布局
  7. 想抢先体验Android操作系统的魅力吗?那就
  8. Android的Activity切换动画特效库SwitchL
  9. Android(安卓)AIDL Service调试方法
  10. android 跳转到横屏activity出现之前acti