android - 警报对话已保护访问权限

标签 android

我正在试验 android 开发,并获取设备的位置。

下面列出了我的 GPSTracker 类并抛出了错误。

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 box
    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;
    }
}

这是我第一次尝试为 android 编写代码。我已经用谷歌搜索了一段时间,但不明白为什么会抛出这个错误。

谁能解释一下这个错误的含义,以及如何修复它。

也将不胜感激详尽的解释,以及指向任何相关谷歌文档的链接。

最佳答案

该错误意味着 AlertDialog 构造函数不可访问(公共(public))。它被声明为 protected ,因此程序员在使用 AlertDialogs 时被迫使用构建器模式。

要显示 AlertDialog,您可以使用 AlertDialog.Builder 设置所有内容,然后调用 show() 来构建和显示 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();

关于android - 警报对话已保护访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25560408/

相关文章:

java - 从RecyclerView迁移到PagedListAdapter,使用submitList不显示数据(Android Studio JAVA)

Android 属性,declare-styleable,引用

android - 在android中来电时隐藏通知栏

android - fragment 崩溃 : java. lang.IllegalArgumentException:找不到 View

android - 在模拟器上使用 c2dm 获取注册 ID

android - 推送通知是否需要 Google Cloud Messaging Service

android - RecyclerView item 点击勾选状态

android - 应用内视频托管的最佳解决方案

android - Universal-Image-Loader 中的任务被中断

android - Google Play 服务中 UserRecoverableAuthException.getIntent() 中的 NullPointerException