Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析符号';定位服务&x27;尝试添加棉花糖权限支持时_Java_Android_Permissions_Gps_Android 6.0 Marshmallow - Fatal编程技术网

Java 无法解析符号';定位服务&x27;尝试添加棉花糖权限支持时

Java 无法解析符号';定位服务&x27;尝试添加棉花糖权限支持时,java,android,permissions,gps,android-6.0-marshmallow,Java,Android,Permissions,Gps,Android 6.0 Marshmallow,我正在尝试创建一个GPSTracker类,我可以从一个片段运行它来获取用户的GPS位置。我有一个运行良好,除了它需要愚蠢的棉花糖许可支持(我知道这并不愚蠢,我实际上喜欢它,但它现在是一个害虫) 我收到两条错误消息。一个“无法解析行中的符号“LocationService” ActivityCompat.requestPermissions( this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION },

我正在尝试创建一个GPSTracker类,我可以从一个片段运行它来获取用户的GPS位置。我有一个运行良好,除了它需要愚蠢的棉花糖许可支持(我知道这并不愚蠢,我实际上喜欢它,但它现在是一个害虫)

我收到两条错误消息。一个“无法解析行中的符号“LocationService”

ActivityCompat.requestPermissions( this, new String[] {  android.Manifest.permission.ACCESS_COARSE_LOCATION  },
                    LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION);
另一个“无法解析方法”showartDialog(me.paxana.alerta.adapter.GPSTracker、java.lang.String、java.lang.String、boolean)”

排队

public void showAlertDialog(){
        am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
GPSTracker.java

package me.paxana.alerta.adapter;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.app.AlertDialog;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import android.app.Activity;

import com.afollestad.assent.Assent;
import com.afollestad.assent.Assent.*;
import com.afollestad.assent.AssentActivity;
import com.afollestad.assent.AssentCallback;
import com.afollestad.assent.PermissionResultSet;


public class GPSTracker extends Service implements android.location.LocationListener {

    private Context context;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location;
    double latitude,longitude;



    LocationManager locationManager;
    AlertDialog.Builder am = new AlertDialog.Builder(this);
    public GPSTracker(Context context){
        this.context = context;
        getLocation();
    }
    private Location getLocation() {
        // TODO Auto-generated method stub
        if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

            ActivityCompat.requestPermissions( this, new String[] {  android.Manifest.permission.ACCESS_COARSE_LOCATION  },
                    LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION);
        }
        try{
            locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


            if (!isGPSEnabled && !isNetworkEnabled){

            } else {
                this.canGetLocation = true;

                if (isNetworkEnabled){

                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 3, this);

                    if (locationManager != null){
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }

                if (isGPSEnabled){
                    if (location == null){
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 3, this);
                        if (locationManager != null){
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null){
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                } else {
                    showAlertDialog();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return location;
    }
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(GPSTracker.this);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.delete);

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
                dialog.cancel();
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    public void showAlertDialog(){
        am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
    }
    public double getLatitude(){
        if (location != null){
            latitude = location.getLatitude();
        }

        return latitude;
    }

    public double getLongitude(){
        if (location != null){
            longitude = location.getLongitude();
        }

        return longitude;
    }

    public boolean canGetLocation(){
        return this.canGetLocation;
    }
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null){
            this.location = location;
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
编辑

我想我把第二个换成了

public void showAlertDialog(){
        AlertDialog.Builder am = new AlertDialog.Builder(GPSTracker.this);

        // Setting Dialog Title
        am.setTitle("GPS settings");

        // Setting Dialog Message
        am.setMessage("GPS is not enabled. Do you want to enable it?");

        am.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        });
        am.setNegativeButton("No", null);
        am .create().show();

出现第一个错误时,我相信您只需要
MY\u PERMISSION\u ACCESS\u COURSE\u LOCATION
,而不需要
LocationService.MY\u PERMISSION\u ACCESS\u COURSE\u LOCATION

至于第二个错误,通过查看,类
AlertDialog.Builder
中没有方法
showAlertDialog


我相信创建对话框的正确方法是使用与
showSettingsAlert()
方法相同的方法。

我从其他地方找到了一个解决方案,首先,只需声明一个变量:

private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11;
并使用以下命令:

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_ACCESS_COARSE_LOCATION);
}

这是我的工作,希望能对你有所帮助。

第一个,当我放弃LocationService,将其设为我的“权限”\u“访问”\u“粗略”\u“位置”(我注意到一个拼写错误!=粗略,这出乎意料地不是问题),然后它无法解析第二个上的符号“我的权限\访问\粗糙\位置”,我明白你的意思,但我不确定在回答“是”时该怎么办,如何使意图打开GPSi认为我找到了第二个,谢谢你的帮助