Android – alert dialogue protects access

I'm trying Android development and getting the location of the device

The following lists my gpstrancker classes and raises an error

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.util.Log;

/**
 * Created by informationservices on 29/08/14.
 */
public class GPSTracker extends Service implements LocationListener {


    private final Context mContext;

    //flag for GPS status
    boolean isGPSEnabled = false;
    //flag for network status
    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location;
    double latitude; //latitude variable
    double longitude; //longitude variable

    //The minimum distance to change updates in meters

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
    private static final long MIN_TIME_BW_UPDATES = 1000;

    //declare location manager
    protected LocationManager locationManager;


    public GPSTracker(Context context){
        this.mContext = context;
        getLocation();
    }

    //function to get latitude
    public double getLatitude(){
        if (location != null){
            latitude = location.getLatitude();

        }
        //must have a return (as its a function)
        return latitude;
    }

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

        return longitude;
    }





    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            //getting GPS Status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //getting Network Status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                //No Network provider is enabled
            }else{
                this.canGetLocation=true;
                //first get location from Network Provider
                if(isNetworkEnabled){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager !=null){
                        location=locationManager.getLastKNownLocation(LocationManager.NETWORK_PROVIDER);
                        if(location !=null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        }
                    }
                }

            }
        }
        catch (Exception e){
            e.printStackTrace();

        }
        return location;
    }

    //check if this is the best network provider
    public boolean canGetLocation(){
        return this.canGetLocation;
    }

    //show GPs settings in alert @R_5_2419@
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog(mContext);//AlertDialog(android.content.Context) has protected access in 'android.app.AlertDialog'

        //set alert title
        alertDialog.setTitle("GPS is Settings");

        //set Dialog message
        alertDialog.setMessage("GPS is not Enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivities(new Intent[]{intent});

            }
        });

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

        //show alert
        alertDialog.show();
    }


    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

This is my first attempt to write Android. I've been searching Google, but I don't understand why it throws this error

Someone can explain the meaning of this error and how to solve it

A thorough explanation and links to any relevant Google documents will also be appreciated

@R_ 419_ 1911@:

This error means that the alertdialog constructor (public) cannot be accessed. It is declared to be protected, so programmers must use builder mode when using alertdialogs

To display AlertDialog, you can use AlertDialog.Builder to set all contents, and then call show () to build and display AlertDialog..

// Use the AlertDialog.Builder to configure the AlertDialog.
AlertDialog.Builder alertDialogBuilder =
        new AlertDialog.Builder(this)
                .setTitle("GPS is Settings")
                .setMessage("GPS is not Enabled. Do you want to go to settings menu?")
                .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivities(new Intent[]{intent});
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

// Show the AlertDialog.
AlertDialog alertDialog = alertDialogBuilder.show();

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>