【问题标题】:how to Auto Enable GPS如何自动启用 GPS
【发布时间】:2015-04-08 05:23:50
【问题描述】:

转到设置 -> 位置服务-> 检查对我的位置的访问,我想以编程方式执行此操作 autoEnable ,我尝试了以下代码但无法正常工作

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");      
intent.putExtra("enabled", true);
sendBroadcast(intent);
private void turnGPSOn(){     
 String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);      if(!provider.contains("gps")){ 
    //if gps is disabled         
    final Intent poke = new Intent();           
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");          poke.addCategory(Intent.CATEGORY_ALTERNATIVE);         poke.setData(Uri.parse("3"));          
    sendBroadcast(poke);     

          } 
}

【问题讨论】:

    标签: android android-intent permissions gps location


    【解决方案1】:

    考虑 API 版本 //启用


     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    sendBroadcast(intent);
    

    //禁用


    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);
    

    在 Android Manifest 中设置权限


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

    More Details Please Visit

    【讨论】:

    • 你能改变 Android Manifest 吗??
    • 是的,我在清单中授予了这些权限。此代码在某些移动设备中工作,但在某些移动设备或标签中不起作用
    • 请考虑 API 版本
    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多