2014-08-29 60 views
2

我正在嘗試使用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。我一直在谷歌搜索,但不明白爲什麼它拋出這個錯誤。

有人能解釋一下這個錯誤的含義,以及如何解決它。

徹底解釋,並鏈接到任何相關的谷歌文檔也將不勝感激。

+0

包含您的堆棧跟蹤。 – mstrthealias 2014-08-29 01:36:04

+1

也許發佈logcat – maffelbaffel 2014-08-29 01:37:03

回答

14

該錯誤表示AlertDialog構造函數不可訪問(public)。它被聲明爲受保護的,因此程序員在使用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(); 
+0

謝謝你的澄清! – scb998 2014-09-01 01:15:50

1

我counterd這個問題,因爲剛纔我正想了解AlertDialog.BuilderAlertDialog的區別 所以當我寫

AlertDialog dialog=new AlertDialog(); 

所以它給了我同樣的問題,像您這樣的 但由於你寫了

AlertDialog.Builder alertDialog = new AlertDialog(mContext);

        //.Builder was missing 
AlertDialog.Builder = new AlertDialog.Builder(mContext) 

,如果你想使用AlertDialog然後做這樣的

AlertDialog = new AlertDialog.Builder(mContext) 

這應該工作。