2016-10-22 110 views
0

我目前正在做一个项目,应用程序将获取用户的当前位置并将其显示在textview中的片段内。我没有得到任何错误,但由于某种原因,它不工作..Android使用LocationListener获取用户当前位置片段内

这是我的代码的一部分。 在清单和进口的所有权限都很好..

另一个奇怪的事情是我的应用程序不会显示我的logcat中的任何东西,当我点击按钮。

注:所有代码片段里面

希望有人可以给我一个提示或两个的感谢!

import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationManager; 
import android.location.LocationListener; 
private LocationManager locationManager; 
private LocationListener listener; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    // Inflate the layout for this fragment 
    View myinflate = inflater.inflate(R.layout.fragment_home, container, false); 
    final TextView cords = (TextView) myinflate.findViewById(R.id.welcome); 
    final Button butt = (Button) myinflate.findViewById(R.id.button1); 

    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
    listener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      cords.setText(location.getLatitude() +" "+location.getLongitude()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    butt.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       return; 
      }else { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
      } 
     } 
    }); 

    return myinflate; 
} 

回答

0

这种情况发生在Android M版或更高版本

块:

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       return; 

指示按钮事件处理函数什么都不做,如果应用程序没有权限。

的解决方法是请求的权限从片段:

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(LocationSurface.PERMISSIONS_LOCATION, AppConstants.REQUEST_CODE_PERMISSIONS); 
      } 
}else{....} 

或者,从仿真器,设置 - >应用程序 - >您的应用程序 - >权限 - >授予位置权限

+0

谢谢回复! – Duckbenok

+0

两个变量LocationSurface和AppConstants无法解析? – Duckbenok

+0

LocationSurface&AppConstants是自定义的变量在我的应用程序 公共静态最终字符串[] PERMISSIONS_LOCATION = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }; public static final int REQUEST_CODE_PERMISSIONS = 2;请参考: https://developer.android.com/training/permissions/requesting.html – ahmedmsvb