作者skywillnosky (Alfred)
看板Eclipse
标题[问题] GPS程式问题
时间Wed Aug 18 19:09:37 2010
今天有打一个用基地台定位的手机程式
目前只有在模拟器上跑过
可是 只要跑到append那里似乎就会出错
出错的意思就是跑出"sorry"的那个视窗
把那部份注解掉程式可跑
以下是完整程式码
.java
//*************************************//
package com.demo.android.mylocation;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import android.location.Criteria;
import android.util.Log;
public class MyLocation extends Activity implements LocationListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateStat();
}
private LocationManager mgr;
private String best;
private void updateStat() {
// TODO Auto-generated method stub
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
Location location = mgr.getLastKnownLocation(best);
StringBuffer msg = new StringBuffer();
msg.append("Latitude: ");
//有问题部份 //msg.append(Double.toString(location.getLatitude())); //**
msg.append(", Longitude: ");
//有问题部份 //msg.append(Double.toString(location.getLongitude())); //**
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Toast.makeText(this, location.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 60000, 1, this);
}
@Override
protected void onPause(){
super.onPause();
mgr.removeUpdates(this);
}
}
还有一个部份 (有改动的)Manifest
//*************************************//
主要改动部份:(都是新增)
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.INTERNET
//*************************************//
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.demo.android.mylocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".MyLocation"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
其他部分是没有改动的
一行一行试的结果确定是append那里出问题
append()直接给予字串不会有错
可是在里面引用其他函数或别的东西
就会出现问题 而去论坛有找过 好向也没有相关文章
都是找到中文出勘误的= =
有哪位神手大大可以给建议吗?
感激不尽
PS:版面有点乱<(_ _)>
刚刚又试了一下发现.getLatitude()这类的函数一用就会出错
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.64.183.231
1F:→ skywillnosky:再补充一件事:这程式本人是完全依照 08/18 19:16
2F:→ skywillnosky:"手机应用程式设计入门"第二版的第39章打的 08/18 19:17
※ 编辑: skywillnosky 来自: 203.64.183.231 (08/18 19:33)
3F:推 freesamael:java程式问题建议可至java版询问(而且那边人比较多) 08/18 19:52
4F:→ ericinttu:android 版也可问android开发事项 08/18 20:00
5F:→ freesamael:android版似乎仍以使用者导向为主,好像不太有开发相关 08/18 21:26
6F:→ afz096:location.getLatitude() 确定有拿到正确的东西?? 08/22 22:26
7F:推 nvidia:没有也可以去问 08/25 16:16
8F:推 yyuto:基地台是GSM定位 GPS是卫星导航 算是不太一样的技术 10/28 00:35