getContext() 和 getActivity() 与 FragmentActivity

2024-01-30

我需要将 getActivity() 和 getContext() 方法与 FragmentActivity 一起使用。怎样制作呢?我无法扩展 Fragment 类(我现在不能这样做)。也许我可以选角或者其他什么。需要在这堂课上做。

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()”。

e.g.

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

可以简单地变成

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

getContext() 和 getActivity() 与 FragmentActivity 的相关文章

随机推荐

  • 如何绑定ToolTip中的其他元素

    我想在工具提示中绑定文本 但我有一个问题 它的绑定值是其他元素控件 因此我基本上无法通过绑定获取它们的值
  • TCP 客户端或服务器卡在处理数据上

    我正在尝试编写一个简单的 TCP 服务器和客户端程序 当我运行下面的服务器和客户端代码时 客户端将简单地从服务器接收时间消息并退出 并且服务器继续接受新连接 我预期的程序行为是我希望服务器也从客户端接收 hello world 消息并关闭连
  • 如何在Python中将SVG图像渲染为PNG文件?

    所以我想从具有目标分辨率WxH的python代码渲染SVG 将SVG文本作为str 像这样 https developer mozilla org en US docs Web SVG Element feDisplacementMap我动
  • Drupal 7 Field API:如何以编程方式发送表单元素的 #ajax 属性中指定的 AJAX 请求?

    我正在使用 Drupal 7 字段 API 通过 AJAX 重新加载部分表单 我有一个可以进行呼叫的按钮 但我想将其删除并以编程方式进行呼叫作为对特定事件的响应 这是我的 AJAX 按钮代码 form documents reload do
  • 资源未找到,但已就位

    我的报告向我提供了此错误 但我不明白这怎么可能 java lang RuntimeException Unable to start activity ComponentInfo net aviascanner aviascanner ne
  • 如何在多个虚拟服务器上运行 Jenkins 作业?

    我已经在服务器上安装了 Jenkins 默认情况下该作业将仅在该服务器上运行 我的要求是来自 Jenkins 如何在多个服务器上运行相同的作业 它将如何 ssh 到其他服务器 我是 Jenkins 的新手 请帮忙 基本上我的目标是将应用程序
  • 尝试将 Google 电子表格库与 TypeScript 一起使用时出现 ERR_OSSL_UNSUPPORTED

    我尝试执行此代码 从文档复制https theoephraim github io node google spreadsheet https theoephraim github io node google spreadsheet as
  • PHP 多维数组排序 array_reverse 不起作用?

    我有一个Multidimensional我正疯狂地尝试按日期正确排序 到目前为止我已经得到 usort respArr function a b t1 strtotime a PublishDate t2 strtotime b Publi
  • 检测对标注标题的点击

    我如何检测到点击title注释的标注 我已经有一个右侧标注配件和一个左侧标注配件 但我想检测用户是否点击标题 位于标注的中心 如果这是不可能的 如何在点击标题时禁用隐藏标注 回答你的问题有点晚了 但我最近正在处理同样的问题 并通过反复试验自
  • 用于检查文件中是否存在段落/行的 Shell 脚本

    我最近编写了一个 bash 脚本来检查文件中是否存在特定段落 文件的内容是 已发表 1EO保存完成贸易节省 贸易保存成功 56945458 220841 b 用于 MCR CMDTY 来自来源 ICE Tradecapture API 重试
  • 我应该使用 printf("\n") 或 putchar('\n') 在 C 中打印换行符吗? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 当我用 C 编写程序时 我经常需要自己打印换行符 我知道你至少可以通过两种方式做到这一点 printf n and putchar n 但我不确
  • Spring Data Repository 不会删除 ManyToOne 实体

    我目前正在尝试使用 Spring 数据存储库来删除我的一些实体 删除调用不会出现任何异常 错误消息 但实体随后不会被删除 这些是我的实体 public class Board implements Serializable Id Gener
  • 嵌入式计算机视觉平台[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我计划在智能手机平台上启动一个基于计算机视觉的项目 I know iPhone http niw at articles 2009 0
  • 检测iOS是否正在使用webapp

    我想知道是否可以检测 iOS 用户是否正在使用 web 应用程序 或者只是使用 safari 浏览器以正常方式访问 我想要实现的原因是 在 iOS Web 应用程序上 当用户单击链接时 他将被重定向到 Safari 浏览器 所以我使用以下解
  • 如何递归地查找嵌套对象中值的键

    我想通过递归找到 Javascript 嵌套对象中值的键 这是我对该功能的尝试 有没有更优雅的方法来实现这个 const foo data data2 data3 worked data21 rand data01 rand01 funct
  • 有没有办法在 ngFor 中声明特定类型?

    我正在使用 ngFor 迭代 Angular 4 x 中特定类型 Menu 的集合 然后循环菜单对象的集合属性 菜单项 不幸的是 该属性在我的 IDE 中未知 Eclipse https en wikipedia org wiki Ecli
  • 根据特定标准删除重复项

    我有一个看起来像这样的数据集 df lt structure list Claim Num c 500L 500L 600L 600L 700L 700L 100L 200L 300L Amount c NA 1000L NA 564L 0
  • Appengine 反向引用 - 需要复合索引吗?

    我有一个最近开始抛出的查询 The built in indices are not efficient enough for this query and your data Please add a composite index fo
  • 在 JavaScript 中重写或扩展闭包函数

    只是想知道如何覆盖闭包函数的行为 例如我怎样才能覆盖baz 下面的代码片段中的函数 功能 var foo Hello var bar World function baz return foo bar 如果您在封闭区域之外 则无法覆盖baz
  • getContext() 和 getActivity() 与 FragmentActivity

    我需要将 getActivity 和 getContext 方法与 FragmentActivity 一起使用 怎样制作呢 我无法扩展 Fragment 类 我现在不能这样做 也许我可以选角或者其他什么 需要在这堂课上做 public cl