gpt4 book ai didi

android - 设计方法 : android and web service

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:50 25 4
gpt4 key购买 nike

我正在为与 json/rest 网络服务通信的 android 手机开发一个应用程序。我需要定期对服务器进行某些类型的调用以检查某些信息。在这种情况下,我可能还需要查询 GPS 的当前位置。我还没有决定使用本地服务,因为我不太清楚如何处理它们,事实上我需要定期检索这些数据并相应地刷新 MapView。我听说我可以在服务中使用 PendingIntents,将此数据关联为有效负载并将它们发送到解压缩数据并刷新 UI 的广播接收器,我还听说这是一个糟糕的设计方法,因为广播接收器是旨在用于。有人有一些有用的提示吗?

最佳答案

首先您必须处理谷歌地图,因为您将显示 map View 。看看这个 Using Google Maps in Android on mobiForge .

其次,您需要一个提供 gps 数据的类。使用消息处理程序获取位置数据和更新 UI 很简单。这是一个例子:

public MyGPS implements LocationListener{

public LocationManager lm = null;
private MainActivity SystemService = null;
//lat, lng
private double mLongitude = 0;
private double mLatitude = 0;

public MyGPS(MainActivity sservice){
this.SystemService = sservice;
this.startLocationService();
}

public void startLocationService(){
this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
}

public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}
}

在您的 onCreate 方法中创建此类的实例,locationlistener 将开始监听 gps 更新。但是您无法访问 lng 和 lat,因为您不知道您的 Activity 是设置的还是空的。因此,您需要一个处理程序,在设置 lat 和 lng 时向您的主要 Activity 发送消息:

修改如下方法:

public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
Message msg = Message.obtain();
msg.what = UPDATE_LOCATION;
this.SystemService.myViewUpdateHandler.sendMessage(msg);
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}

在您的主要 Activity 中添加:

Handler myViewUpdateHandler = new Handler(){

public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LOCATION:
//access lat and lng
}));
}

super.handleMessage(msg);
}
};

由于处理程序在您的 mapactivity 中,您可以在处理程序本身中轻松更新您的 UI。每当 gps 数据可用时,处理程序就会触发并接收消息。

开发 REST API 是一件非常有趣的事情。一种简单的方法是在网络服务器上有一个 php 脚本,该脚本根据请求返回一些 json 数据。如果您想开发这样的服务,本教程可能会对您有所帮助,link .

关于android - 设计方法 : android and web service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3778676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com