android - 如何通知用户启用gpsprovider

标签 android

我正在研究gpsprovider,如果用户单击基于gps的应用程序作为假设 map 应用程序,它会检查gps提供商是否启用,如果不提醒用户启用gpsprovider。

将会有一个广播接收器。 将定义一条消息,用于从任何应用程序请求服务。如果未启动,将启动服务。 将定义一条消息来声明应用程序不再需要它。如果没有应用程序需要它,将停止服务

最佳答案

  String provider = Settings.Secure.getString(getContentResolver(),     Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider != null ){      
        if(! provider.contains("gps")){    
            // Notify users and show settings if they want to enable GPS
            new AlertDialog.Builder(MessagePage.this)
            .setMessage("GPS is switched off. enable?")
            .setPositiveButton("Enable GPS", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, 5);
                }
            })
            .setNegativeButton("Don't do it", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .show();
        }
    }

      protected void onActivityResult(int requestCode, int resultCode, Intent data){
       if(requestCode == 5 && resultCode == 0){
           String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
           if(provider != null){
               switch(provider.length()){
               case 0:                     
                    //GPS still not enabled..
                   break;
               default:
                   Toast.makeText(this, "GPS is now enabled.", Toast.LENGTH_LONG).show();
                   break;
               }
           }
       }
       else{
           //the user did not enable his GPS
       }
}

关于android - 如何通知用户启用gpsprovider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033307/

相关文章:

android - 403 :The request is understood, 但已被拒绝 Twitter Android

android - 如何使用 jetpack compose 以编程方式在按钮单击时打开外部 url?

android - 通过 Intent 录制视频,录制视频的大小为0

java - 如何将使用 GZIP 压缩的字符串从 Java App 发送到 PHP Web 服务

android - 使用通用图像加载器内存不足错误查看寻呼机

android - eclipse 3.5 + ADT : build path errors

android - 使用 WorkManager 上传大型位图

android - 如何使用 Android SQLite 数据库在 iOS 中填充 CoreData?

android - 提交登录后 deezer android api 重定向到空白页面

java - 无法从 DrawerLayout 项目检查当前 Activity