android定位已获取经纬度,上传google解析后textview不显示,基础问题,求大神解答~~>_<`

来源:互联网 发布:evga显卡怎么样 知乎 编辑:程序博客网 时间:2024/06/05 04:45
各位大神好,小弟自学安卓半年多,现在需要做一个点击后定位的界面,由于电脑比较烂,调试有困难,以下源码有些是网上找的,自己又凑了凑,大体解释下:
        Location_main类中上半截获取到经纬度没啥问题,然后加了个经纬度上传获取地理位置(message)就不行了,最后的message输出为空;
        由于网络需要线程操作,不太会弄,就设置message全局变量,然后输出textview试过用runOnUiThread()方法也不显示,初步判断问题好像是在new Thread 这里。(单独用获取经纬度正常,单独用网址拼接上传返回值也合适,就是合起来出问题),我基础比较差,可能是哪里的逻辑出错了,烦请大神帮忙看看,小弟感激不尽~~


Location_main源码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Window;
import android.widget.TextView;

import com.example.dailyreport.R;

public class Location_main extends Activity {

private TextView text;
private ProgressDialog dialog;
private String message = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.location);
text = (TextView) findViewById(R.id.location_text);

// 注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
this.registerReceiver(new LocationBroadcastReceiver(), filter);

// 启动服务
Intent intent = new Intent();
intent.setClass(this, LocationSvc.class);
startService(intent);

// 等待提示
dialog = new ProgressDialog(this);
dialog.setMessage("正在转圈圈...");
dialog.setCancelable(true);
dialog.show();
}

private class LocationBroadcastReceiver extends BroadcastReceiver {
/**
 * 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用
 * Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 来实现的。
 */
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Common.LOCATION_ACTION))
return;
String locationInfo = intent.getStringExtra(Common.LOCATION);
String getSignInfo = locationInfo.substring(
locationInfo.indexOf("network") + 7,
locationInfo.indexOf("acc"));// 截取经纬度
final String Longitude = locationInfo.substring(
locationInfo.indexOf("network") + 7,
locationInfo.indexOf(","));
final String Latitude = locationInfo.substring(
locationInfo.indexOf(",") + 1, locationInfo.indexOf("acc"));
// 经纬度转换物理地址
new Thread(new Runnable() {
/**
 * 发送经纬度到google服务器
 */
@Override
public void run() {
String url = "http://maps.google.cn/maps/api/geocode/json?latlng="
+ Latitude + "," + Longitude + "&language=CN";
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
message = message + data;
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

Time t1 = new Time();
t1.setToNow();
int year = t1.year;
int month = t1.month + 1;
int day = t1.monthDay;
int hour = t1.hour;
int minute = t1.minute;
int second = t1.second;
text.setText("考勤成功!" + "\n" + year + "-" + month + "-" + day + "\n"
+ hour + ":" + minute + ":" + second + "\n" + "\n"
+ getSignInfo + "\n" + message);
//打印message为空!!!!!!
dialog.dismiss();
Location_main.this.unregisterReceiver(this);// 不需要时注销
}
}
}



---------------------------------------------------------------------------------------------------------------------

两个工具类:
LocationSvc.java

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * @author SunnyCoffee
 * @version 1.0
 * @desc 定位服务
 * 
 */
public class LocationSvc extends Service implements LocationListener {

private static final String TAG = "LocationSvc";
private LocationManager locationManager;

@Override
//分别发送一个呼叫一个IBinder对象和接收来电给Binder对象
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}

@Override
public void onStart(Intent intent, int startId) {
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}

@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}

public void onLocationChanged(Location location) {
Log.d(TAG, "Get the current position \n" + location);

//通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LOCATION, location.toString());
sendBroadcast(intent);

// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。

locationManager.removeUpdates(this);
stopSelf();
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

}

---------------------------------------------------------------------------------------------------------------------
Common.java

public class Common {

public static final String LOCATION = "location";
public static final String LOCATION_ACTION = "locationAction";
}



--------------------------------------------------------------------------------------------------------------------
最终输出结果:

考情成功!
2016-3-28
16:34:14
36.0666645,103.777221
(这的实际位置信息就显示不出来了。。。)



0 0
原创粉丝点击