2017-07-05 3 views
-1

私は数日間検索してきましたが、問題の原因を見つけることができません。私はボタンAをクリックしているときの私の現在の位置を知りたいと思うし、ボタンBをクリックしているときの私の位置を知っている。問題はこれを、私はBをクリックするとうまくいくようだが、Aではない。それらの両方で正確に同じコードが、ここで Android GPSサービスは毎回0.0を返します

A.

のためのいくつかの方法で、それだけで文句を言わない仕事は私のGetGPSLocationコード `

package com.example.eandl.gps; 

import android.Manifest; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.provider.Settings; 
import android.support.v4.app.ActivityCompat; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 



public class GetGPSLocation { 
    private Context context; 
    private static double lat; 
    private static double lang; 

    public GetGPSLocation(Context context){ 
     this.context = context; 
     this.lat = 0.0; 
     this.lang = 0.0; 
    } 



    public void setLat(double lat) { 
     this.lat = lat; 
    } 



    public void setLang(double lang) { 
     this.lang = lang; 
    } 

    public Boolean displayGpsStatus() { 
     final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
      return false; 
     }else{ 
      return true; 
     } 
    } 

    public double[] getLatLong(){ 
     double[] latLong = new double[2]; 
     if(displayGpsStatus()){ 

      LocationManager locationMangaer = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 
      LocationListener locationListener = new MyLocationListener(context, this); 

      if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       ActivityCompat.requestPermissions((Activity)context,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
      } 
      if(checkLocationPermission()){ 
       locationMangaer.requestLocationUpdates(LocationManager 
         .NETWORK_PROVIDER, 0, 0, locationListener); 
      } 

      Log.d("findError", getLang() + ""); 
      latLong[0] = getLat(); 
      latLong[1] = getLang(); 

     }else{ 
      alertbox("Gps status", "Your Device's GPS is Disable"); 
     } 


     return latLong; 
    } 

    public boolean checkLocationPermission() 
    { 
     String permission = "android.permission.ACCESS_FINE_LOCATION"; 
     int res = context.checkCallingOrSelfPermission(permission); 

     return (res == PackageManager.PERMISSION_GRANTED); 
    } 


    protected void alertbox(String title, String mymessage) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setMessage(mymessage) 
       .setCancelable(false) 
       .setTitle(title) 
       .setPositiveButton("Gps On", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // finish the current activity 
           // AlertBoxAdvance.this.finish(); 
           Intent myIntent = new Intent(
             Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
           context.startActivity(myIntent); 
           dialog.cancel(); 
          } 
         }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // cancel the dialog box 
           dialog.cancel(); 
          } 
         }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

} 

`

そしてListiener

package com.example.eandl.gps; 

import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 



public class MyLocationListener implements LocationListener { 

    private Context context; 
    private GetGPSLocation getGPSLocation; 


    public MyLocationListener(Context context, GetGPSLocation getGPSLocation) { 
     this.context = context; 
     this.getGPSLocation = getGPSLocation; 
    } 


    @Override 
    public void onLocationChanged(Location loc) { 
     if(loc != null){ 
      getGPSLocation.setLat(loc.getLatitude()); 
      getGPSLocation.setLang(loc.getLongitude()); 
     } 
    } 



    @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 
    } 
} 
です

私がそれを呼び出そうとしているのは、新しいGetGPSLocation(context).getLatLongですが、私が言ったように時々それは私の位置を時々返します0.0

ありがとう!

+0

同じコードを正確に使用するとうまくいくはずです。私はあなたがちょうど何かを監督したと思う。どのようにあなたがそれを使用して私たちを表示してください... – Opiatefuchs

+0

明らかに、私はとても愚かであり、ボタンBの標準であったものをチェックするのを忘れてしまった、そして今私がそうしたときBはどちらもうまくいきません...愚かな私。 – Deggo

答えて

0

さてさて、私はこの

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    //Fixa vit med blå text 
    myView = inflater.inflate(R.layout.fragment_signup, container, false); 
    placements = new ArrayList<CheckBox>(); 
    GetPlacements getPlacements = new GetPlacements(this); 
    getPlacements.execute(); 

    getGPSLocation = new GetGPSLocation(myView.getContext()); 
    getGPSLocation.getLatLong(); 

ように私のビューを作成するときに最初に私はそれを設定していそして、これはボタンを使用しています私のdatabassenderに行く

   getGPSLocation.getLatLong(); 
       DatabasSender databasSender = new DatabasSender(myView.getContext()); 
       User user = new User(name.getText().toString(), email.getText().toString(), 0); 
       user.setSignup(true); 
       user.setEdu(edu.getText().toString()); 
       user.setPhone(phone.getText().toString()); 
       user.setPlacements(places); 
       databasSender.execute(user); 

onclicklistenerですa AsyncTask

public DatabasSender(Context context) { 
    this.context = context; 
    getGPSLocation = new GetGPSLocation(context); 

} 

@Override 
protected void onPreExecute() { 
    latLong = getGPSLocation.getLatLong(); 
} 
@Override 
protected Object doInBackground(Object[] objects) { 
    urlPatten = "..../"+latLong[0]+"&"+latLong[1]; 
} 

ボタンBと全く同じコードを使用します。

関連する問題