【问题标题】:prompt the user to activate gps in a background service?提示用户在后台服务中激活 GPS?
【发布时间】:2017-04-04 17:30:02
【问题描述】:

第一次安装应用程序时主要活动被破坏,之后我有一个后台服务,只要有可用的互联网就运行。

后台服务会检测位置(除其他外),并且由于该应用程序应该在手机上保留几个月(它仅在有互联网时才有效,而不是一直有效,否则会进一步耗尽电池电量)好吧,因为它运行了很长时间,用户可以禁用 gps 并忘记在下次连接到互联网之前重新打开它,这可能导致应用程序在 google API 客户端请求位置时崩溃,所以现在,后台应用程序在检测到 gps 已关闭时会自动打开设置界面以提醒用户打开它,但这个解决方案并不是那么好,因为界面突然出现并且用户可能会感到困惑。

所以我想制作一个提示/警报对话框,告诉用户“应用程序 [appName] 需要 gsp 请打开它”,选择“是”和“否”,当用户点击“是”时,设置界面如图。这是可行的吗?同样,我不是在寻找具有活动的解决方案,因为我已经知道如何做到这一点(仅使用服务也没有透明活动)

这是我目前的工作方式:

//This part of the code is right before getting the current location
    if (!isLocationEnabled(this)) {

              Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(myIntent);

        }

    public static boolean isLocationEnabled(Context context) {
        int locationMode = 0;
        String locationProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;

        } else {
            locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }


    }

【问题讨论】:

  • public void requestCamera(Callback callback) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.CAMERA)) != PackageManager.PERMISSION_GRANTED) { callbacks.put(33,callback); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 33); } else { callback.call(true); } }
  • @HenningLuther 我很困惑。 “相机”与我的问题有什么关系?我正在寻找提示用户激活 gps 的解决方案
  • 是相机的例子,每个权限都是一样的,你只需要找到你的 GPS 权限的全局常量并改变它。我很困惑,你看不到这一点。只是懒得给你找,如果不给我20秒,也许你可以自己找到。
  • 好吧。我将尝试按照您的示例并使用 GPS 权限做同样的事情
  • 好!看来你是个学生,所以自己去发现是一件好事,并且学习如何去做。你也不需要回调的东西,它只是为了在并发场景中同步,所以对你来说它可能不是必需的。尝试删除并更改为您需要的所有内容。

标签: android geolocation gps android-alertdialog android-permissions


【解决方案1】:

你可以在你的服务中实现

Location. Locationllistener 

并使用覆盖方法

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {// here if provider is gps show notification } }; // Register the listener with the Location Manager to 

在此方法中,您可以发出警报声通知

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多