簡體   English   中英

在Android手機上測試GPS當前位置

[英]Testing GPS current location on Android mobile phone

何時,我在Android手機上測試此代碼,所以當前位置未顯示。 我應該配置手機還是將新代碼增加到代碼中,以使其能夠在手機上正常運行。 提示將不勝感激。 代碼如下

public class GetLocation extends Activity {

    TextView tv;
    double latitude,longitude,accuracy;
    private  LocationManager lm;
    private LocationListener lo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)this.findViewById(R.id.txtLocation);
        lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lo = new mylocationlistener();
        tv.setText("waiting for location");
        if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){

            createGpsDisabledAlert();  

          }

    }


    //dialog to check if gps enabled or not
    private void createGpsDisabledAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setMessage("Your GPS is disabled! Would you like to enable it?")
        .setCancelable(false) 
        .setPositiveButton("Enable GPS", 
        new DialogInterface.OnClickListener(){ 
            public void onClick(DialogInterface dialog, int id){  
                 showGpsOptions();  
            } 
        });
        builder.setNegativeButton("Do nothing",  
                 new DialogInterface.OnClickListener(){  
                 public void onClick(DialogInterface dialog, int id){
                     dialog.cancel();  
                   }
                    }); //close negative builder

        AlertDialog alert = builder.create();  
        alert.show();  
             }


    private void showGpsOptions(){  
         Intent gpsOptionsIntent = new Intent(  
         android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
         startActivity(gpsOptionsIntent);  

    }

     private class mylocationlistener implements LocationListener {

        public void onLocationChanged(Location location) {


            if (location !=null){


            longitude = location.getLongitude();
            latitude = location.getLatitude();
            accuracy = location.getAccuracy();
                  /*
                    Log.d("LOCATION CHANGED", latitude + "");
                    Log.d("LOCATION CHANGED", longitude + "");

                    */ String str = "\n CurrentLocation: "+
                        "\n Latitude: "+ latitude + 
                        "\n Longitude: " + longitude + 
                        "\n Accuracy: " + accuracy; 

                      Toast.makeText(GetLocation.this,str,Toast.LENGTH_LONG).show();
                      tv.append(str);               

            }

        }

        public void onProviderDisabled(String provider) {
             Toast.makeText(GetLocation.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show();

        }

        public void onProviderEnabled(String provider) {
             Toast.makeText(GetLocation.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
             Toast.makeText(GetLocation.this,"onStatusChanged",Toast.LENGTH_LONG).show();
        }

}

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

    }


    public void onResume(){
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lo);  
        super.onResume();


    }



}

在onCreate中,您需要調用lm.requestLocationUpdates()來使位置偵聽器正常工作。 請參閱請求位置更新

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM