带有FragmentActivity的getContext()和getActivity()

编程入门 行业动态 更新时间:2024-10-28 17:22:52
本文介绍了带有FragmentActivity的getContext()和getActivity()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在FragmentActivity中使用getActivity()和getContext()方法.怎么做?我不能扩展Fragment类(我现在不能做).也许我可以投下其他东西.需要在本堂课上做.

I need to use getActivity() and getContext() methods with FragmentActivity. How to make it? I can't extends Fragment class(i can't to do now). Maybe I can cast or something else. Need to do it in this class.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest; private LatLng latLng; private Marker currLocationMarker; private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; buildGoogleApiClient(); } private synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this.getContext()) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } public void onConnected(@Nullable Bundle bundle) { if (ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(getActivity(), new String[]{ android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.ACCESS_FINE_LOCATION }, 100); return; } Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { //place marker at current position //mGoogleMap.clear(); latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title("Current Position"); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); currLocationMarker = mMap.addMarker(markerOptions); CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(latLng, 5); mMap.animateCamera(yourLocation); } mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(5000); //5 seconds mLocationRequest.setFastestInterval(3000); //3 seconds mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter } }

推荐答案

getActivity()和getContext()严格是Fragment方法.由于FragmentActivity类扩展了Activity类,因此分别为"this"和"getApplicationContext()".

getActivity() and getContext() are strictly Fragment methods. Since the FragmentActivity class extends the Activity class, the alternatives are 'this' and 'getApplicationContext()' respectively.

例如

mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())

可以简单地变成

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())

更多推荐

带有FragmentActivity的getContext()和getActivity()

本文发布于:2023-11-25 16:42:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FragmentActivity   getContext   getActivity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!